2

我想知道如何使用木偶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;

    }

);

项目视图

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: {

            }
        });
    });

集合视图

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

1 回答 1

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(){

            }

        });
于 2013-04-11T12:36:09.660 回答