我尝试了以下方法为我的模型设置一个id:
var globalCounter = 1; var Model = Backbone.Model.extend({ initialize: function () { this.id = globalCounter; globalCounter += 1; } }); myModel = new Model(); console.log(myMode.get('id')); // prints undefined@H_301_3@如何为我的模型设置ID?
解决方法
您需要使用
set()
功能(
http://jsbin.com/agosub/1/);
var globalCounter = 1; var Model = Backbone.Model.extend({ initialize: function () { this.set('id',globalCounter); globalCounter += 1; } }); myModel = new Model(); console.log(myModel.get('id')); // prints 1@H_301_3@