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

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

问题描述

让我们说我有:

  VAR书= Backbone.Model.extend();VAR收集= Backbone.Collection.extend({
  模式:书,
  网址:'/书籍,
  初始化:功能(){
    this.fetch();
  })
})

我怎样才能修改收藏网​​址实例化一个新的集合时?

VAR AdventureBooks =新的图书(网址:{url:'/书籍/冒险'})不起作用

  VAR AdventureBooks =新的图书({类:'冒险'})

和在Collection定义:

网​​址:'/书籍/+ this.category 也不起作用。

感谢。


解决方案

  VAR书= Backbone.Model.extend({
  URL:功能(){
    回报/书籍/+ this.get(类别);
  }
});

let's say I have :

var Book = Backbone.Model.extend();

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

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

var AdventureBooks = new Books({ url: '/books/adventure' }) does not work

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

and in the Collection definition:

url : '/books/' + this.category does not work either.

Thanks.

解决方案

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

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

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