admin管理员组

文章数量:1430346

Using Backbone.js I need to handle an array of strings returned from a JSON web service. Should I create a Backbone Collection to fetch 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've created this simple Collection to fetch the data:

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

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

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

It looks like Backbone is trying to parse the strings as a model instead of seeing that it's just a raw string. How can I get the data into a Collection so I can use it in a View?

Using Backbone.js I need to handle an array of strings returned from a JSON web service. Should I create a Backbone Collection to fetch 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've created this simple Collection to fetch the data:

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

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

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

It looks like Backbone is trying to parse the strings as a model instead of seeing that it's just a raw string. How can I get the data into a Collection so I can use it in a View?

Share Improve this question edited Oct 11, 2014 at 10:01 James 25.6k13 gold badges93 silver badges135 bronze badges asked Mar 6, 2012 at 17:06 spaetzelspaetzel 1,2623 gold badges16 silver badges23 bronze badges 2
  • Collections ARE collections of Models. There's no concept of a Collection of anything other then Models in Backbone. – Edward M Smith Commented Mar 6, 2012 at 17:15
  • I understand that. The question is how should I fetch an array of strings using Backbone? It's easy using jQuery.ajax, but I want to be consistent about how I interact with the web service. – spaetzel Commented Mar 6, 2012 at 17:36
Add a ment  | 

2 Answers 2

Reset to default 3

If you just need the strings, your best bet might be to just let jQuery (or Zepto--whatever has the $) handle the heavy lifting:

var names = [];

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

After the fetch pletes, the names variable will be populated with the results of your query.

This doesn't make a lot of sense, since backbone collection is designed to be, well, a collection of models. However, you could override the parse method with your own parser.

本文标签: javascriptBackbone handle array of stringsStack Overflow