主干网和 Rails 嵌套路由

发布于 2024-12-18 15:08:17 字数 216 浏览 0 评论 0原文

我在 Rails 中定义了以下路由:

resources :accounts do
  resources :transactions
end

这会产生类似以下的 url:

/accounts/123/transactions/1

有没有一种简单的方法可以将其映射到设置的主干模型?

I have the following routes defined in rails:

resources :accounts do
  resources :transactions
end

This results in urls like:

/accounts/123/transactions/1

Is there an easy way to map this to a backbone model set up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

戈亓 2024-12-25 15:08:17

事实证明,骨干很容易通过在模型中嵌套集合来支持这一点,如下所示:

var Account = Backbone.Model.extend({

  initialize: function() {
    this.transactions = new TransactionsCollection;
    this.transactions.url = '/account/' + this.id + '/transactions';
    this.transactions.bind("reset", this.updateCounts);
  },
});

这完全达到了我想要的效果。

您可以在这里阅读更多相关信息:http://documentcloud.github.com/backbone/#FAQ-nested

Turns out backbone quite easily supports this by nesting a collection in a model as follows:

var Account = Backbone.Model.extend({

  initialize: function() {
    this.transactions = new TransactionsCollection;
    this.transactions.url = '/account/' + this.id + '/transactions';
    this.transactions.bind("reset", this.updateCounts);
  },
});

This achieves exactly what I wanted.

You can read more about it here: http://documentcloud.github.com/backbone/#FAQ-nested

阳光①夏 2024-12-25 15:08:17

这可能不是一个简单的方法,但我认为最好的方法是使用 url 并将其设置为这样的函数:

var Transaction = Backbone.Model.extend({
    url: function(){
        var url = 'accounts/"+this.account_id+"/transactions';
        if (this.id !== undefined){
            url += "/"+this.id
        }
        return url;
    }
});

或者也许在咖啡脚本中(因为它是backbone+rails):

class Transaction extends Backbone.Model
    url: -> 
        url = "accounts/#{@account_id}/transactions"
        url += "/#{@id}" if @id != undefined
        url

哦,你可以这样做更多这样的(当然嵌套越深越好):

var url = ["accounts", this.account_id, "transactions"]
if (this.id !== undefined) url.push(this.id)
return url.join("/")

据我所知,主干中现在有 url 实用程序,这对我来说还不够痛苦,所以我会在其他库中搜索一个:)

It might not be an easy way but I think the best way is to use url and set it to a function like this:

var Transaction = Backbone.Model.extend({
    url: function(){
        var url = 'accounts/"+this.account_id+"/transactions';
        if (this.id !== undefined){
            url += "/"+this.id
        }
        return url;
    }
});

Or maybe in coffeescript (as it is backbone+rails):

class Transaction extends Backbone.Model
    url: -> 
        url = "accounts/#{@account_id}/transactions"
        url += "/#{@id}" if @id != undefined
        url

Oh and you could do it more like this(surely with deeper nesting it's better):

var url = ["accounts", this.account_id, "transactions"]
if (this.id !== undefined) url.push(this.id)
return url.join("/")

AFAIK there is now url utility in backbone, and it's not much enough pain for me so that I would search for one in some other library :)

爱*していゐ 2024-12-25 15:08:17

Backbone 不直接支持创建嵌套 url。您必须使用函数来动态计算嵌套对象的结果 url。例如:

var Account = Backbone.Model.extend({

  initialize: function() {
    this.transactions = new TransactionsCollection();
    var self = this;
    this.transactions.url = function () {return self.url + self.id + '/transactions';};
    // etc
  },
});

更多信息: http://documentcloud.github.com/backbone/#FAQ-nested

Backbone does not directly support the creating of nested urls. You must use a function to dynamically calculate the resulting url of your nested object. For example:

var Account = Backbone.Model.extend({

  initialize: function() {
    this.transactions = new TransactionsCollection();
    var self = this;
    this.transactions.url = function () {return self.url + self.id + '/transactions';};
    // etc
  },
});

More information: http://documentcloud.github.com/backbone/#FAQ-nested

被你宠の有点坏 2024-12-25 15:08:17

只需定义您的模型或(如果您使用一个)集合的 url,如下所示:

var MyModel = Backbone.Model.extend({
  url: 'accounts/123/transactions'
});

或动态:

mymodel.url = 'accounts/' + accountId + '/transactions';

以这种方式配置的那些模型或集合的所有模型现在将相应地生成其后端 url。

详细信息:

型号:
http://documentcloud.github.com/backbone/#Model-url

集合:
http://documentcloud.github.com/backbone/#Collection-url

Just define the url of your model or (if you use one) your collection like so:

var MyModel = Backbone.Model.extend({
  url: 'accounts/123/transactions'
});

or dynamically:

mymodel.url = 'accounts/' + accountId + '/transactions';

Those models or all models of a collection that is configured this way will now generate it's backend urls accordingly.

Detailed info:

Model:
http://documentcloud.github.com/backbone/#Model-url

Collection:
http://documentcloud.github.com/backbone/#Collection-url

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文