2013-04-11 25 views
2

我想知道如何使用木偶collectionView。我正在使用https://github.com/marionettejs/backbone.marionette作爲樣板。 目前我無法看到集合視圖呈現的任何內容。如何使用木偶collectionView。我使用https://github.com/marionettejs/backbone.marionette作爲樣板

骨幹收集

define(["jquery","backbone","models/Store"], 
    function($, Backbone, Store) { 
    // Creates a new Backbone Collection class object 
    var StoreCollection = Backbone.Collection.extend({ 
     // Tells the Backbone Collection that all of it's models will be of type Model (listed up top as a dependency) 
     model: Store 
    }); 

    storeCollectionObj = new StoreCollection(); 
    storeCollectionObj.add({"title":"wherwr werwe"}); 
    return storeCollectionObj; 
    }); 

型號

define(["jquery", "backbone"], 
    function($, Backbone) { 
     // Creates a new Backbone Model class object 
     var Store = Backbone.Model.extend({ 

      // Model Constructor 
      initialize: function() { 

      }, 

      // Default values for all of the Model attributes 
      defaults: { 

      }, 

      // Get's called automatically by Backbone when the set and/or save methods are called (Add your own logic) 
      validate: function(attrs) { 

      } 

     }); 

     // Returns the Model class 
     return Store; 

    } 

); 

ItemView控件

define(['underscore', 'jquery', 'handlebars', 'text!templates/merchant/store.html'], 
    function(_, $, Handlebars, template) { 
     //ItemView provides some default rendering logic 
     return Backbone.Marionette.ItemView.extend({ 
      //Template HTML string 
      template: Handlebars.compile(template), 
      id: "store", 
      attributes: function() { 
       return {class :"storeView"} 
      }, 

      // View Event Handlers 
      events: { 

      } 
     }); 
    }); 

CollectionView

define(['underscore', 'jquery', 'handlebars' , 'views/merchant/StoreView','text!templates/merchant/storeCollection.html' , 'collections/StoreCollection'], 
    function(_, $, Handlebars, SroreView, template, StoreCollection) { 
     SroreViewObj = new SroreView(); 
     //storeCollectionObj = new StoreCollection(); 
     //alert(StoreCollection); 
     //StoreCollection.add({"title":"sdfsadfasd"}); 
     //ItemView provides some default rendering logic 
     StoreCollectionView = Backbone.Marionette.CollectionView.extend({ 
      id : "StoreListing", 
      template: Handlebars.compile(template), 
      itemView: SroreViewObj, 
      collection : StoreCollection, 
      render: function(){ 

      } 

     }); 

     return StoreCollectionView; 
    }); 

回答

0

我發現了我的錯誤。我在我的集​​合視圖中傳遞了itemView對象。

itemView: SroreViewObjitemView: SroreView

只是通過用 「SroreView」(ItemView控件定義)

SroreViewObj = new SroreView(); 
     //storeCollectionObj = new StoreCollection(); 
     //alert(StoreCollection); 
     //StoreCollection.add({"title":"sdfsadfasd"}); 
     //ItemView provides some default rendering logic 
     StoreCollectionView = Backbone.Marionette.CollectionView.extend({ 
      id : "StoreListing", 
      template: Handlebars.compile(template), 
      itemView: SroreViewObj, // This should not be an object. So fixed just by replacing "SroreViewObj" with "SroreView" (itemView definition)** 
      collection : StoreCollection, 
      render: function(){ 

      } 

     }); 
取代 「SroreViewObj」 固定