English 中文(简体)
Backbone: Creat Collection from JSON
原标题:Backbone: Create collection from JSON

I m 试图将JSON(从Candards s json_encode)装入Abone JS藏书。 我将问题简化为:

var myJSON =  [{ "id":"1","name":"some name","description":"hmmm"}] ;

var myCollection = new MyCollection(myJSON, { view: this });

而且:

MyObject = Backbone.Model.extend({

  id: null,
  name: null,
  description: null
});

MyCollection = Backbone.Collection.extend({ 
model: MyObject,
initialize: function (models,options) { }
});

错误:

Uncaught Type错误:Cannot use in operator to search for id in

类似问题: Backbone: fetch Collection from服务器

我的“智者”当然似乎以适当的形式出现,我是否失踪了明显的东西? 我曾尝试用简单称道:“1”而不是“id”,结果相同。

最佳回答

Your JSON is still in string format. Pass it to JSON.parse before assigning it:

var myJSON = JSON.parse( [{"id":1,"name":"some name","description":"hmmm"}] );
问题回答

页: 1

MyObject = Backbone.Model.extend({
  defaults: {
    id: null,
    name: null,
    description: null
  }
});

因此,可能完全没有问题,但问题似乎只是阵列上的单一引语。 这就是:

var myJSON =  [{ "id":"1","name":"some name","description":"hmmm"}] ;

should be:

var myJSON = [{ "id":"1","name":"some name","description":"hmmm"}];

Php, afik, doesn t add the single quotes, so it should be as simple as changing a line that says:

$my_js = "var myJSON =  " . json_encode($my_array_to_send)) . " ;";

:

$my_js = "var myJSON = " . json_encode($my_array_to_send)) . ";  ";




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签