Backbonejs传递从一个数据到其他看法? [英] Backbonejs pass data from one to other view?

查看:261
本文介绍了Backbonejs传递从一个数据到其他看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有我有两个观点...我需要从一个视图到下一个放数据,但我不知道我怎么能实现这一点。

There I have two views ... I need put data from one view to next but I dont know how can I implement this .

有第一次的功能视图:

var CategoriesView = Backbone.View.extend({ 
    initialize:function () {
    this.render();
},
render:function () {
    var that = this;
    var categories = new Categories();
    categories.fetch({
        success: function (categories) {
        var template = _.template($('#categories-template').html(), {categories: categories.models});
          that.$el.html(template);
        }
    })
}             
});

有模板是:

    <script type="text/template" id="categories-template">
        <% _.each(categories, function(category) { %>
            <li><%= category.get('name') %></li> 
        <% }); %>
    </script>

型号和系列:

var Category = Backbone.Model.extend({
defaults: {
    name: 'Category',
    id: []
},
});
var Categories = Backbone.Collection.extend({
url: 'api/categories.json'
});

所有的JSON对象(所有种类)我在名称:

All names from json objects (all category) I have in :

<li>there</li> 

...要每个类别,我需要实现产品....有我的产​​品列表:

...To each category I need implement Products .... there I have list of products :

var ProductsView = Backbone.View.extend({ 
    initialize:function () {
    this.render();
},
render:function () {
    var that = this;
    var products = new Products();
    products.fetch({
        success: function (menus) {
        var template = _.template($('#products-template').html(), {products: products.models});
          that.$el.html(template);
        }
    })
}             
});

模板:

    <script type="text/template" id="products-template">
        <% _.each(products, function(product) { %>
                <li class="productscls"><%= product.get('name') %></li> 
        <% }); %>
    </script>

示范和coollection:

Model and coollection:

var Product = Backbone.Model.extend({
defaults: {
    name: 'Product',
    category: 1,
    id: []
},
});
var Products = Backbone.Collection.extend({
url: 'api/products.json'
});

这一切我的东西是正确的表现,但是当我试图让类似有:

All this my stuff is showed correct but when I try to make something like there :

    <script type="text/template" id="categories-template">
        <% _.each(categories, function(category) { %>
                <li class="categorycls"><%= category.get('name') %></li>
            <% _.each(products, function(product) { %>
                    <li class="productscls"><%= product.get('name') %></li>
            <% }); %>
        <% }); %>
    </script>

所以你可以看到我想要做。对于每个类别每个产品,但没有将数据传递到类别或产品查看该功能不...

So there you can see what I wanna make. For each category each product but this not function without passing data into category or products view....

问候Makromat

Regards Makromat

推荐答案

我想你会想要做绕类别和产品的意见,然后将其子视图父包装视图。我还要做初始化方法取我的,而不是在渲染。我想你可以做的是使用类别视图呈现具有所有与占位符由ID每个产品类别的元素。然后使用作为产品视图模板,每个迭代产品,并在匹配(凭身份证)品类占位元素插入。这可能工作。 pretty复杂的东西。在所有这是否有意义?

I think you will want to do a parent wrapper view around the Category and Product views, which are then subviews. I would also do my fetching in the initialize methods, not in the renders. I think what you could do is use the Category view to render an element that has all the categories with placeholders for each Product by Id. Then use that as a template for the Product view, iterate each Product and insert it in the matching (by Id) Category placeholder element. That could work. Pretty complex stuff. Does this make sense at all?

另一个想法可能是一个数据库,认为变平这种关系。然后用一个BB观点与基于该数据库视图的集合。

Another idea might be a database view that "flattens" this relationship. Then use one BB view with a collection based on that database view.

第二届想法可能是艰难与不知道的许多产品是如何的类别。第一个想法肯定会工作。构建有充分的分类与编号标记占位符的模板,然后传递作为模板到产品图片,在每个类别中插入产品列表中标识的占位符。

2nd idea might be tough with not knowing how many products are within the categories. First idea will definitely work. Build a template that has every Category with Id marked placeholders, then pass that as the template to Product view, insert the list of Products at each Category Id placeholder.

这篇关于Backbonejs传递从一个数据到其他看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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