backbone.js change url parameter of a Model and fetch does not update data fetched

后端 未结 4 506
再見小時候
再見小時候 2021-02-04 10:28

I have the following Model:

window.MyModel = Backbone.Model.extend({
     initialize: function(props){
          this.url = props.url;
     } 

     parse: funct         


        
4条回答
  •  一整个雨季
    2021-02-04 10:43

    model.urlRoot = "/your/url"
    

    OR

    model.urlRoot = function(){ return "/your/url"; }
    

    OR

    model.url = "/your/url"
    

    OR

    model.url = function(){ return "/your/url"; }
    

    Default 'url' property of a Backbone.Model object is as below. Backbone.js doc says:

    Default URL for the model's representation on the server -- if you're using Backbone's restful methods, override this to change the endpoint that will be called.

    url: function() {
      var base = getValue(this, 'urlRoot') || getValue(this.collection, 'url') || urlError();
      if (this.isNew()) return base;
      return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.id);
    },
    

    Clearly, It first gets the value of urlRoot, if not available, it will look at the url of the collection to which model belongs. By defining urlRoot instead of url has an advantage of falling back to collection url in case urlRoot is null.

提交回复
热议问题