Parse.com框架(基于Backbone)model.set在循环收集时不起作用

时间:2012-11-07 18:38:23

标签: javascript backbone.js parse-platform

当循环遍历集合中的结果时,当我立即尝试使用get检查值时,设置模型属性似乎不会成功。我错过了什么?

collection.each(function(model){


    var objectId = model.id.toString();

    model.set({'objectId':objectId}, {silent:true});                    

    console.log('model.id = ' + model.id + ' and model.get("objectId")' + model.get('objectId'));

    // model.get('objectId') returns 'undefined' but model.id returns proper id



});

如代码中所述,model.get('objectId')返回'undefined'但是model.id返回正确的id ...

2 个答案:

答案 0 :(得分:1)

以下代码

var TestCollection = Backbone.Collection.extend({
    model: Backbone.Model
});

var collection = new TestCollection;
collection.reset([{id: 1}, {id: 2}, {id: 3}]);

collection.each(function(model) {
  var objectId = model.id.toString();
  model.set({'objectId': objectId}, {silent: true});                    
  console.log('model.id %s  and model.get("objectId") %s', model.id, model.get('objectId'));
})

输出

model.id 1  and model.get("objectId") 1 
model.id 2  and model.get("objectId") 2 
model.id 3  and model.get("objectId") 3 

因此我假设实际问题隐藏在其余代码中。你是否压倒标准的Backbone行为?

答案 1 :(得分:0)

我发现Parse框架不允许你设置“objectId”的属性。