Backbone model url method

时间:2015-07-28 17:08:38

标签: backbone.js

When I set idAtribute of a model I expect it to be used in url. Is it the right thing to expect or am I missing something? I see that backbone model sees 'id' attribute on itself and uses it to build url, but I explicitly told it to use '_id' property. It doesn't look like the right behavior to me.

var model = new Backbone.Model();
model.set('id', 1); // if you remove this line everything works properly
model.set('_id', 2);
model.idAttribute = '_id';
model.urlRoot = 'models';
model.url(); // returns "models/1"

1 个答案:

答案 0 :(得分:1)

我会这样做。

var someModel = Backbone.Model.extend({

    initialize: function(attr,params) {
        this.id = attr.id;
        this.url= "somerooturl/"+params.id;     
    }

});

var model = new someModel(attr,params);

model.set ...