How do I fetch a single model in Backbone?

前端 未结 7 1100
误落风尘
误落风尘 2020-11-30 18:36

I have a Clock model in Backbone:

var Clock = Backbone.Model.extend({});

I\'m trying to get an instance of that that has the l

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 19:08

    Your second approach is the approach I have used. Try adding the following to your Clock model:

    url : function() {
      var base = 'clocks';
      if (this.isNew()) return base;
      return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
    },
    

    This approach assumes that you have implemented controllers with the hashbang in your URL like so, http://www.mydomain.com/#clocks/123 , but it should work even if you haven't yet.

提交回复
热议问题