json - Backbonejs pass data from one to other view? -


there have 2 views ... need put data 1 view next dont know how can implement .

there first function view :

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

there template :

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

model , collection :

var category = backbone.model.extend({ defaults: {     name: 'category',     id: [] }, }); var categories = backbone.collection.extend({ url: 'api/categories.json' }); 

all names json objects (all category) have in :

<li>there</li>  

...to each category need implement products .... there have list of products :

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

template :

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

model , coollection:

var product = backbone.model.extend({ defaults: {     name: 'product',     category: 1,     id: [] }, }); var products = backbone.collection.extend({ url: 'api/products.json' }); 

all stuff showed correct when try make 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 can see wanna make. each category each product not function without passing data category or products view....

regards makromat

i think want parent wrapper view around category , product views, subviews. fetching in initialize methods, not in renders. think use category view render element has categories placeholders each product id. use template product view, iterate each product , insert in matching (by id) category placeholder element. work. pretty complex stuff. make sense @ all?

another idea might database view "flattens" relationship. use 1 bb view collection based on database view.

2nd idea might tough not knowing how many products within categories. first idea work. build template has every category id marked placeholders, pass template product view, insert list of products @ each category id placeholder.


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -