主干视图事件在渲染后未触发

时间:2012-12-20 10:45:04

标签: jquery backbone.js

我有像这样的骨干视图

define(['backbone','text!templates/settings.html'], function( Backbone, SettingsTemplate) {
var SettingsView = Backbone.View.extend({
    el:"#settings-screen",

    viewTemplate: _.template(SettingsTemplate),

    events: {
        "click button#save-settings":"save"
    },

    initialize: function() {

    },

    render: function() {
        $("#game-viewport").html(this.viewTemplate);
    },

    save: function(event) {
        console.log(event);
    }
});

return SettingsView;

});

这是视图通过我的路由器初始化

define(['backbone','views/settings'], function(Backbone,SettingsView) {

    var Router = Backbone.Router.extend({

        routes: {
            "newgame":"showGameview",
            "settings":"showSettingsview"
        },

        switchView: function(view) {
            if(this.currentView)
                this.currentView.close()

            this.currentView = view;
            this.currentView.render();
        },

        showGameview: function() {

        },

        showSettingsview: function() {
            var view = new SettingsView;
            this.switchView(view);
        }
    });
    return Router;
});

这会启动应用

require.config({

    paths: {
        'backbone':'./libs/backbone',
        'underscore':'./libs/underscore-min',
        'text':'./plugins/text',
        'domReady':'./plugins/domReady'
    },
    shim:{
        'backbone': {
            deps: ['underscore'],
            exports: 'Backbone'
        }
    }
});

require(['domReady','views/start','routers/router'], function(domReady, StartView, Router) {
    domReady(function() {
        var view = new StartView;
        view.render();

        var router = new Router();
        Backbone.history.start();
    });
});

但在渲染视图后,事件不会激发。可能有什么不对?

0 个答案:

没有答案