Backbone.js的[渲染上单击视图。 /用户操作] [英] Backbone.js [ Rendering a view on a click. / user action ]

查看:130
本文介绍了Backbone.js的[渲染上单击视图。 /用户操作]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器端的URL给了我所有类别列表通过的http://主机:3000 / categories.json

The server side URL gives me the list of all categories via http://host:3000/categories.json

我在类别中的骨干和放大器的模型;我在追赶subCategoriesCollection子类。在这个时间点的URL为的http://主机:3000 /分类/ 1

I'm having category as a model in backbone & I'm catching sub-categories in subCategoriesCollection. At this point of time the URL is http://host:3000/category/1

这会给我的所有子类的集合下,1。

this would give me a collection of all sub-categories under 1.

我要的是,为了在页面上显示每个类别。也就是说,如果有10个大类,我需要渲染视图10倍,允许用户显示一个页面上的每个类别,但使用相同的模板。

what I want is that, to display Each category on a page. That is if there are 10 categories , I need to render view 10 times allowing user to display each category on single page but using same template.

请指导我一下吧。

推荐答案

这是使用子视图呈现集合的典型方法(我假定这就是你的。参见当你说相同的模板来)。

This is a typical way to render a collection using a sub view (I'm assuming that's what your refering to when you say "same template").

var SubCategoryView = Backbone.View.extend({
    initialize: function(){
        _(this).bindAll();
    },
    render: function () {
        $(this.el).html(this.model.escape('Name');
        return this;
    }
});
var SubCategoryListView = Backbone.View.extend({
    initialize: function(){
        _(this).bindAll();
    },
    render: function () {
        $(this.el).empty();
        this.collection.each(function (subcategory)
        {
            var childView = new SubCategoryView({
                model: subcategory
            });
            $(this.el).append(childView.render().el);
        });

        return this;
    }
});

然后,你可以通过你的子类别征集到SubCategoryListView

Then you can render the category view by passing your sub category collection to the SubCategoryListView

我发现这个链接是有帮助的,当我与骨干开始了。这是深入多一点,但提供了什么,我刚才输入一个更强大的解决方案:的 http://liquidmedia.ca/blog/2011/02/backbone-js-part-3/

I found this link to be helpful when I was starting out with backbone. It's a little more in-depth but provides a more robust solution that what I just typed in: http://liquidmedia.ca/blog/2011/02/backbone-js-part-3/

好运

这篇关于Backbone.js的[渲染上单击视图。 /用户操作]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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