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

前端 未结 5 1343
-上瘾入骨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:37

    One approach is to override Backbone.sync() method, either for all classes or for just your class. However, presumably your goal is to override fetch for just a single model. One way to do that is to directly call jQuery.ajax(...), and on success, take the response and set that, e.g.

    fetchByName: function() {
       var self = this;
    
        $.ajax({
          url: self.urlRoot+ "?name="+this.get('name'),
          type: 'GET',
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function(data) {
              self.set(data);
          }
        });
    
    
    }
    

提交回复
热议问题