如何设置集合的 url [英] How to set a Collection's url

查看:21
本文介绍了如何设置集合的 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有:

var Book = Backbone.Model.extend();

var Collection = Backbone.Collection.extend({
  model: Book,
  url: '/books',
  initialize: function(){
    this.fetch();
  })
})

如何在实例化新集合时更改 Collectionurl ?

How can I change the Collection's url when instantiating a new collection ?

var AdventureBooks = new Books({ url: '/books/adventure' }) 不起作用

var AdventureBooks = new Books({ category: 'adventure' })

并在集合定义中:

url : '/books/' + this.category 也不起作用.

谢谢.

推荐答案

var Book = Backbone.Model.extend({
  "url": function() {
    return '/books/' + this.get("category");
  }
});

这篇关于如何设置集合的 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆