路由和按钮单击无法在Backbone + Handlebars中工作

时间:2014-05-08 06:25:01

标签: javascript jquery backbone.js handlebars.js

的index.html

<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="js/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="js/lib/bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
        <link href="js/lib/ratchet/ratchet-theme-ios.css" rel="stylesheet">
        <link href="js/lib/ratchet/ratchet.css" rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>totter</title>
    </head>
    <body>
        <div class="content">
        </div>
        <script src="js/lib/jquery-1.9.1.min.js"></script>
        <script src="js/lib/underscore-min.js"></script>
        <script src="js/lib/backbone-min.js"></script>
        <script src="js/lib/bootstrap/js/bootstrap.js"></script>
        <script src="js/lib/handlebars/handlebars-v1.3.0.js"></script>
        <script src="js/lib/ratchet/ratchet.js"></script>
        <script src="js/common/helper.js"></script>
        <script src="js/app.js"></script>
        <script src="js/views/home.js"></script>
        <script src="js/views/signin.js"></script>
        <script src="js/models/home.js"></script>
        <script src="js/models/signin.js"></script>
    </body>
</html>

app.js

var app = {
    views: {},
    models: {},
    loadTemplates: function(views, callback) {

        var deferreds = [];

        $.each(views, function(index, view) {
               if (app[view]) {
                   deferreds.push($.get('template/' + view + '.hbs', function(data) {
                            app[view].prototype.template = _.template(data);
                            }, 'html'));
               } else {
                   alert(view + " not found");
               }
        });

        $.when.apply(null, deferreds).done(callback);
    }
};


app.Router = Backbone.Router.extend({

    routes: {
      "": "home",
     "signIn":"SignIn"
    },

    home: function () {
    // Since the home view never changes, we instantiate it and render it only once
      if (!app.home) {
          app.home = new app.HomeView();
          app.home.render();
      } else {
        // delegate events when the view is recycled
          app.home.delegateEvents();
      }
    },
    SignIn:function(){
        if (!app.signin) {
            app.signin = new app.SignInView();
            app.signIn.render();
        } else {
        // delegate events when the view is recycled
            app.signin.delegateEvents();
        }
    }
});


$(document).on("ready", function () {
        app.loadTemplates(["HomeView"],
        function () {
            app.router = new app.Router();
            Backbone.history.start();
    });
});

home.js

app.HomeView = Backbone.View.extend({
        //Calling the render method to render view from the template
        initialize:function(){
            this.render();
        },

        //Pass the handlebars template for complilation and
        render: function () {
            var path = './template/HomeView.hbs';
            Helper.GET_TEMPLATE(path, function(template) {

            //pass collection to template to set values
            var html = template(app.homeCollection.toJSON());

            //pass collection to template to set values
            $('.content').html(html);
        });

        $('.signin').bind('click', function(e) {
                           app.Router.navigate("signIn", {trigger: true});
                           });
        }

});

app.home = new app.HomeView();

sigin.js

app.SigInView = Backbone.View.extend({
        //Calling the render method to render view from the template
        initialize:function(){
            this.render();
        },

        //Pass the handlebars template for complilation and
        render: function () {
            var path = './template/SignInView.hbs';
            Helper.GET_TEMPLATE(path, function(template) {

            //pass collection to template to set values
            var html = template(app.signinCollection.toJSON());

            //pass collection to template to set values
            $('.content').html(html);
        });



        }

});

app.signin = new app.SigInView();

helper.js

var Helper = {};

Helper.GET_TEMPLATE = function(path,callback){
    var source, template;
    $.ajax({
        url: path,
        success: function(data) {
            source    = data;

            //Compile the raw data we got from the file
            template  = Handlebars.compile(source);

            //execute the callback if passed
            if (callback){
                callback(template);
            }
        }
    });

}

homeModel.js

var HomeModel = Backbone.Model.extend();

var homeData = new HomeModel({
    id: 1,
    signUpTitle: 'Sign Up for TOT ',
    signInTitle: 'Sign In',
    slogan:'slogan slogan slogan slogan slogan slogan slogan slogan slogan slogan '
});

/**
* Defining a Collection to set model
*/
var HomeCollection = Backbone.Collection.extend({

    model: HomeModel

});

/**
 * Defining a array to hold the collection
 */

app.homeCollection = new HomeCollection([homeData]);

HomeView.hbs

<header class="bar bar-nav">
    <h1 class="title">totter</h1>
</header>
<div class="logo">
    <img src = "img/choice.png">
        </div>

{{#each []}}
<div class="textcontent">
    <label>{{this.slogan}}</label>
</div>
<div class="footer">
    <button class="btn btn-primary btn-block signup" style="">{{this.signUpTitle}}</button>
</div>
<div class="footer">
    <button class="btn btn-primary btn-block signin">{{this.signInTitle}}</button>
</div>
{{/each}}

在上面的代码中,我用来开发一个包含2个视图的简单应用程序。我想在按钮点击登录时显示登录视图。

我怎么能做到这一点?我正在使用“把手”和“backbone.js”。

1 个答案:

答案 0 :(得分:0)

由于您正在使用View el属性,因此事件未触发。要么给它一个预先存在的元素,要么将el本身插入到DOM中。

请参阅以下discussion的答案,以获得更全面的解释。