如何使用木偶集合查看。我使用https://github.com/marionettejs/backbone.marionette作为样板

时间:2013-04-11 10:37:33

标签: marionette

我想知道如何使用木偶集合查看。我使用https://github.com/marionettejs/backbone.marionette作为样板。  目前,我无法看到集合视图呈现的任何内容。

Backbone Collection

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;
    });

1 个答案:

答案 0 :(得分:0)

我发现了自己的错误。我在我的集​​合视图中传递itemView对象。

itemView: SroreViewObj应为itemView: SroreView

修复只需将“SroreViewObj”替换为“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(){

            }

        });