Why title return null !! , is there any confusion with anything !
为什么标题返回null! ,有什么混淆!
Here is my code
这是我的代码
_.each(collection.models, function(element, index, list){
console.log(JSON.stringify(element)); //{"title":"Dipped Bunny Blossom","id":49,"created_at":"2015-03-24T10:16:17Z","updated_at":"2015-03-24T13:56:12Z","type":"simple","status":"publish","downloadable":false,"virtual":false,"permalink":"http://beta-it.com/ticarttest/shop/dipped-bunny-blossom/","sku":"","price":"50.00","regular_price":"50.00","sale_price":null,"price_html":"<span class=\"amount\">$50.00</span>","taxable":false,"tax_status":"taxable","tax_class":"","managing_stock":false,"stock_quantity":0,"in_stock":true,"backorders_allowed":false,"backordered":false,"sold_individually":false,"purchaseable":true,"featured":true,"visible":true,"catalog_visibility":"visible","on_sale":false,"weight":"1.00","dimensions":{"length":"50","width":"50","height":"50","unit":"cm"},"shipping_required":true,"shipping_taxable":true,"shipping_class":"","shipping_class_id":null,"description":"","short_description":"","reviews_allowed":true,"average_rating":"4.50","rating_count":2,"related_ids":[34,43,45,47,120],"upsell_ids":[],"cross_sell_ids":[],"parent_id":0,"categories":["BIRTHDAY","BUSINESS GIFTS","DIPPED FRUIT"],"tags":[],"images":[{"id":50,"created_at":"2015-03-03T10:16:10Z","updated_at":"2015-03-03T10:16:10Z","src":"http://beta-it.com/ticarttest/wp-content/uploads/2015/03/Dipped-Bunny-Blossom.jpg","title":"Dipped-Bunny-Blossom","alt":"","position":0}],"featured_src":"http://beta-it.com/ticarttest/wp-content/uploads/2015/03/Dipped-Bunny-Blossom.jpg","attributes":[],"downloads":[],"download_limit":0,"download_expiry":0,"download_type":"","purchase_note":"","total_sales":1,"variations":[],"parent":[]}
console.log(element.id); // return 49 ok
console.log(element.title); //return null !!!!
// We are looping through the returned models from the remote REST API
// Implement your custom logic here
});
1 个解决方案
#1
For a backbone collection, your code should look more like this:
对于主干集合,您的代码看起来应该更像这样:
collection.each(function(model) {
console.log(model.attributes);
console.log(model.id);
console.log(model.get('title'));
});
Most attributes in backbone models must be accessed using the get
method. id
is a special case that can be accessed via get
or as a direct id
property. According to the docs:
必须使用get方法访问骨干模型中的大多数属性。 id是一种特殊情况,可以通过get或作为直接id属性访问。根据文件:
If you set the id in the attributes hash, it will be copied onto the model as a direct property.
如果在属性哈希中设置id,它将作为直接属性复制到模型中。
#1
For a backbone collection, your code should look more like this:
对于主干集合,您的代码看起来应该更像这样:
collection.each(function(model) {
console.log(model.attributes);
console.log(model.id);
console.log(model.get('title'));
});
Most attributes in backbone models must be accessed using the get
method. id
is a special case that can be accessed via get
or as a direct id
property. According to the docs:
必须使用get方法访问骨干模型中的大多数属性。 id是一种特殊情况,可以通过get或作为直接id属性访问。根据文件:
If you set the id in the attributes hash, it will be copied onto the model as a direct property.
如果在属性哈希中设置id,它将作为直接属性复制到模型中。