How to fetch a Backbone.js model by something other than the ID?

前端 未结 5 1328
-上瘾入骨i
-上瘾入骨i 2021-02-15 14:48

Backbone.js\'s default, RESTful approach to fetching a model by the ID is easy and straight-forward. However, I can\'t seem to find any examples of fetching a model by a differe

5条回答
  •  南方客
    南方客 (楼主)
    2021-02-15 15:29

    You can try doing something like this on your base model definition or on demand when calling fetch.

    model.fetch({ data: $.param({ someParam: 12345}) });
    

    In your case, along the lines of.

    var Widget = Backbone.Model.extend({
        initialize: function(options) {
            this.name = options.name;        
        },
        urlRoot: '/widgets',
        fetchByName: function(){ 
            this.fetch({ data: $.param({ name: this.name }) }) 
        }
    });
    
    var foowidget = new Widget({name: 'Foo'});
    foowidget.fetchByName();
    

提交回复
热议问题