javascript 在backbone.js 中获取模型的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9819510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:56:48  来源:igfitidea点击:

Get attributes of model in backbone.js

javascriptajaxrestbackbone.js

提问by jongbanaag

I have this model

我有这个模型

var Item = Backbone.Model.extend({
   url: 'http://localhost/InterprisePOS/Product/loaditembycategory/Event Materials'
});

var onSuccess = function(){ alert("success"); };

And a collection

和一个集合

var Items = Backbone.Collection.extend({
    model: Item
});

And the rest of my code is here:

我的其余代码在这里:

var item = new Item();
var items = new Items();
item.fetch({ success: onSuccess });
alert(items.get("ItemCode"));

What I want is to simply get the attributes of the model. Now I have this on firebug. Also when I run it on the browser I get the alert successand the next alert is undefined. enter image description here

我想要的是简单地获取模型的属性。现在我在萤火虫上有这个。此外,当我在浏览器上运行它时,我收到警报成功并且下一个警报是undefined在此处输入图片说明

This is the output:

这是输出:

{"ErrorMessage":null,"Items":[{"ErrorMessage":null,"CategoryCode":"Event Materials","ClassCode":null,"Components":null,"GroupCode":null,"ImageURL":null,"ItemCode":"ITEM-123","ItemDescription":"Old World Lamppost\u000d\u000a\u000d\u000a","ItemName":"GET123","ItemType":null,"KitItem":null,"Matrix":null,"Prefix":null,"RetailPrice":107.990000,"SalesTaxCode":null,"UPCCode":null,"UnitMeasureCode":"EACH","UnitsInStock":0,"Value":null,"WholesalePrice":95.000000}]}

NOTE

笔记

That is just one of the items it returns. I just posted on item so that it won't be that long.

那只是它返回的项目之一。我刚刚发布在项目上,所以它不会那么长。

回答by jlb

You are calling geton your collection ( see http://documentcloud.github.com/backbone/#Collection-get)

您正在调用get您的收藏(请参阅http://documentcloud.github.com/backbone/#Collection-get

It seems what you really want is to iterate over the collection, and call get on each item

看来您真正想要的是迭代集合,并在每个项目上调用 get

items.each(function(item) {
  item.get('ItemCode');
});

If not, please elaborate!

如果不是,请详细说明!

Additionally, if your model url responds with a list of models, you should be defining the url attribute in your Collection instead of your model.

此外,如果您的模型 url 以模型列表作为响应,您应该在集合中而不是模型中定义 url 属性。

var Items = Backbone.Collection.extend({
    model: Item,
    url: 'http://localhost/InterprisePOS/Product/loaditembycategory/Event Materials'
});

And your response should be an array, with Items as array elements [<item1>, <item2>, ...], rather than a JSON object with {'Items': [<item1>, <item2>, ...] }. If you don't want to modify the response, you will have to implement the parsefunction on your collection ( http://documentcloud.github.com/backbone/#Collection-parse).

并且您的响应应该是一个数组,其中 Items 作为数组元素[<item1>, <item2>, ...],而不是带有 .json 的 JSON 对象{'Items': [<item1>, <item2>, ...] }。如果您不想修改响应,则必须parse在您的集合 ( http://documentcloud.github.com/backbone/#Collection-parse)上实现该功能。

Also, as @chiborg mentions, you are calling getright after fetch(which will complete asynchronously), so there is no guarantee that your data will be available.

此外,正如@chiborg 所提到的,您会get在之后立即调用fetch(这将异步完成),因此无法保证您的数据可用。

The proper solution here is to bind a 'refresh' listener on the collection.

此处正确的解决方案是在集合上绑定一个“刷新”侦听器。

items.on('refresh', function() {
  // now you have access to the updated collection
}, items);

回答by chiborg

This is due to the model loading asynchonously - item.get("ItemCode")will only work after the model has been loaded with fetch. Try calling it in your onSuccess function.

这是因为模型是异步加载的——item.get("ItemCode")只有在模型加载了fetch. 尝试在您的 onSuccess 函数中调用它。

Additionally, note that it won't help to initialize Itemdirectly. What you are trying to do is get an element in the collection Itemsand then call item.get("ItemCode")on that element, like this:

此外,请注意,Item直接初始化无济于事。您要做的是获取集合中的一个元素Items,然后调用item.get("ItemCode")该元素,如下所示:

function onSuccess() {
   alert('success')
   var item = items.get(13); // get item with id 13
   alert(item.get("ItemCode"));
}