0

我第一次拿起骨干,我在让我的视图呈现我的收藏时遇到了一些麻烦。

main.js

/*global require*/
'use strict';

require.config({
    shim: {
        underscore: {
            exports: '_'
        },
        backbone: {
            deps: [
                'underscore',
                'jquery'
            ],
            exports: 'Backbone'
        },
    },
    paths: {
        app: 'app',
        jquery: '../components/jquery/jquery',
        backbone: '../components/backbone-amd/backbone',
        underscore: '../components/underscore-amd/underscore',
        competitions: 'collections/competition-collection',
        competitionModel: 'models/Competition-model',
        templates: 'templates'
    }
});

require([
    'backbone',
    'app',
    'competitions',
    'competitionModel',
    'views/competition-view',
    'templates'
], function (
    Backbone,
    App,
    Competitions,
    CompetitionModel,
    CompetitionsView
    ) {
        window._app = new App(
            Competitions,
            CompetitionModel,
            CompetitionsView
        );

        window._app.demoData();
        window._app.start();
});

应用程序.js

define([], function() {

    var App = function(Competitions,CompetitionModel,CompetitionsView) {
        // Our models will be instantiated later as needed later.
        this.Models.CompetitionModel = CompetitionModel;
        this.Collections.Competitions = Competitions;
        this.Collections.competitions = new Competitions();
        this.Views.competitionsView = new CompetitionsView();


        //console.log(this.Views.competitionsView)
    };
    App.prototype = {
        Views: {},
        Models: {},
        Collections: {},
        start: function() {

            this.Views.competitionsView.render();
            Backbone.history.start();
        },
        // TODO: We'll get rid of this or move later ... just "spiking" ;)
        demoData: function() {
            var me = new this.Collections.Competitions(
                [
                    {
                        name: 'Some Name',
                    },
                    {
                        name: 'Other Name',
                    }
                ]
            );

            console.log("***** Demo Competitions Created *****");
        }
    };
    return App;
});

竞赛模型.js

define([
    'underscore',
    'backbone',
], function (_, Backbone) {
    'use strict';

    var CompetitionModel = Backbone.Model.extend({
        defaults: {
        },
        initialize: function(){
            console.log(this.attributes);
        }
    });
    this.listenTo(Competitions, 'add', function(){
        console.log("bla")
    });
    return CompetitionModel;
});

竞争-collection.js

define([
    'underscore',
    'backbone',
    'models/competition-model'
], function (_, Backbone, CompetitionModel) {
    'use strict';

    var CompetitionCollection = Backbone.Collection.extend({
        model: CompetitionModel
    });

    return CompetitionCollection;
});

竞争视图.js

define([
    'jquery',
    'underscore',
    'backbone',
    'templates',
    'competitions',
], function ($, _, Backbone, JST, Competitions) {
    'use strict';

    var CompetitionView = Backbone.View.extend({
        template: JST['app/scripts/templates/competition.ejs'],
        render: function() {
            console.log(this.model);
          }
    });
    console.log("yo")


    return CompetitionView;
});

我知道模型已正确加载,但我似乎无法弄清楚如何将模型集合传递给视图并渲染所有对象。

任何人都可以帮忙吗?谢谢

4

1 回答 1

1

您已正确创建视图、集合和模型,但尚未在集合和视图之间创建链接。您需要将集合传递给视图并在视图中使用该集合来呈现所有模型。

在您的 app.js 中替换:

this.Views.competitionsView = new CompetitionsView();

和:

this.Views.competitionsView = new CompetitionsView({collection: this.Collections.competitions});

现在您的视图中有一个集合对象的引用。现在在 Competition-view.js 里面替换:

console.log(this.model);

和:

this.collection.each(function (model) {
    console.log(model);
});

同样在您的 app.js 中,在 start 函数中,您正在调用 Backbone.history.start() 而不创建 Backbone 路由器,这也会导致控制台错误。

于 2013-05-04T03:48:07.630 回答