Array of string strings array

Using Backbone.js I need to process an array of strings returned from a JSON web service. Should I create a Backbone collection to get this list? Here is the data I get from the web service:

["Question Designer","Adaptive Question Designer","Clickable image map","Essay","Fill in the blanks","Maple-graded","Matching","Mathematical formula","Multipart question","Multiple choice","Multiple selection","Numeric","Palette-based symbolic editor","True/false","Other"]

I created this simple collection to retrieve data:

 var questionTypesCollection = Backbone.Collection.extend({ url: function() { return apiBase + '/questions/types'; } }); 

But when I try to get () the collection, I get this error:

 Uncaught TypeError: Cannot use 'in' operator to search for 'id' in Question Designer 

Backbone seems to be trying to parse strings as a model, instead of seeing that it's just a string. How can I get data into a collection so that I can use it in a view?

+4
source share
2 answers

If you just need strings, the best option might be just that jQuery (or Zepto - everything that has $ ) handles the hard work:

 var names = []; $.get(apiBase + '/questions/types', {}, function(result){ names = result; }) 

After the selection is complete, the names variable will be populated with the results of your query.

+3
source

This does not make much sense, since the collection of basic sets is intended to be a collection of models. However, you can override the parse method using your own parser.

+2
source

Source: https://habr.com/ru/post/1400058/


All Articles