0

2 つのコレクション ビューがあります。このビューは、products.json の各オブジェクトと、categories.json から 2 番目の各オブジェクトをレンダリングします<li><%= title %></li>。今、各カテゴリをレンダリングして、カテゴリのすべての製品を含めたい...

// !すべてのカテゴリ ビュー

App.Views.Categories = Backbone.View.extend({
    tagName: 'ul',
    className: 'categories',
    initialize: function() {
      this.collection.on('add', this.addOne, this);  
    },
    render: function() {
      this.collection.each( this.addOne, this );
      return this;
    },
    addOne: function(category) {
        var categoryView = new App.Views.Category({ model: category });
        this.$el.append(categoryView.render().el);
    },
});

// !単一カテゴリ ビュー

App.Views.Category = Backbone.View.extend({
    tagName: 'li',
    className: 'category',
    template: template('allCategoryTemlate'),
    initialize: function() {
      this.model.on('destroy', this.unrender, this);
      this.model.on('change', this.render, this);
    },
    render: function() {
      this.$el.html( this.template( this.model.toJSON() ) );
      return this;
    },
});

// !すべての製品ビュー

App.Views.Products = Backbone.View.extend({
    tagName: 'ul',
    className: 'products',
    initialize: function() {
      this.collection.on('add', this.addOne, this);  
    },
    render: function() {
      this.collection.each( this.addOne, this );
      return this;
    },
    addOne: function(product) {
        var productView = new App.Views.Product({ model: product });
        this.$el.append(productView.render().el);
    },
});

// !SINGLE PRODUCT VIEW

App.Views.Product = Backbone.View.extend({
    tagName: 'li',
    className: 'product',
    template: template('allProductTemlate'),
    initialize: function() {
      this.model.on('destroy', this.unrender, this);
      this.model.on('change', this.render, this);
    },
    render: function() {
      this.$el.html( this.template( this.model.toJSON() ) );
      return this;
    },
});

私の製品:

[   
    {"product_id": 1, "category": "CATEGORY1", "title": "Product 1", "price": 12},
    {"product_id": 2, "category": "CATEGORY1", "title": "Product 2", "price": 12},
    {"product_id": 3, "category": "CATEGORY1", "title": "Product 3", "price": 12},
    {"product_id": 4, "category": "CATEGORY2", "title": "Product 4", "price": 12},
    {"product_id": 5, "category": "CATEGORY2", "title": "Product 5", "price": 12}
]

私のカテゴリー:

[
    {"category_id": 1, "value": "CATEGORY1", "title": "Category 1"},
    {"category_id": 2, "value": "CATEGORY2", "title": "Category 2"},
]

私が必要なものの例:

Category 1
    - Product 1 
    - Product 2 
    - Product 3 

Category 2
    - Product 4 
    - Product 5 

これは私のアプリケーションにとって非常に重要なので、すべての意見に非常に喜んでいます. 次のようなものを作成できますか:

var filtered = products.filter(function(product) {
    return product.get('category') == 'and there I need value from category ?';
});

または、次のようなテンプレートで:

<% _.each(categories, function(category) { %>
    <%= category.get('title') %>
    <li><%= title %></li>

    <% _.filter(products, function(product){ %>
        <%= return product category == value; %>

            <%= product.get('title') %>

            <% }); %> 
        <% }); %>

    <% }); %> 
<% }); %>

どうもありがとうございました !!!

4

1 に答える 1