Backbone:从 JSON 创建集合

发布于 2024-12-26 22:58:15 字数 761 浏览 0 评论 0原文

我正在尝试将 JSON(来自 php 的 json_encode)加载到 Backbone 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) { }
});

错误:

未捕获类型错误:无法使用“in”运算符在中搜索“id”

未捕获的类型错误:无法使用“in”运算符在类似问题 :Backbone:获取来自服务器的集合

我的 JSON 显然格式正确,我是否遗漏了一些明显的东西?我尝试使用简单的 id: "1" 而不是 "id" 得到相同的结果。

I'm attempting to load JSON (from php's json_encode) into a Backbone JS collection. I've simplified the problem to:

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

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

And:

MyObject = Backbone.Model.extend({

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

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

Error:

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

Similar Issue: Backbone: fetch collection from server

My JSON certainly appears to be in the right format, am I missing something obvious? I have attempted using simply id: "1" as opposed to "id" with the same result.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

佞臣 2025-01-02 22:58:15

您的 JSON 仍然是字符串格式。在分配之前将其传递给 JSON.parse:

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

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"}]');
笑饮青盏花 2025-01-02 22:58:15

您忘记了模型中的 defaults 哈希值。

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

请参阅文档

You forgot the defaults hash in your model.

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

See the documentation

雨后彩虹 2025-01-02 22:58:15

所以我可能完全错过了这一点,但问题似乎是数组周围的“单引号”。也就是说,这个:

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

应该是:

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

Php,afik,不添加单引号,所以它应该像将一行:更改

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

为:

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

so i maybe missing the point completely but the problems seems to be the 'single quotes' around the array. That is, this:

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)) . "';";

to:

$my_js = "var myJSON = " . json_encode($my_array_to_send)) . ";  ";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文