2014-02-18 3 views
0

Я новичок в js. У меня есть index.htmlМагистраль и требуют js

<html> 
    <head> 
     <meta charset="utf-8"> 
     <link rel="stylesheet" href="/common.css"/> 
    </head> 
    <body> 
     <div id="page"></div> 
     <script data-main="js/main" src="js/lib/require.js"></script> 
    </body> 
</html> 

main.js

require.config({ 
    urlArgs: "_=" + (new Date()).getTime(), 
    baseUrl: "js", 
    paths: { 
     jquery: "lib/jquery", 
     underscore: "lib/underscore", 
     backbone: "lib/backbone" 
    }, 
    shim: { 
     'backbone': { 
      deps: ['underscore', 'jquery'], 
      exports: 'Backbone' 
     }, 
     'underscore': { 
      exports: '_' 
     } 
    } 
}); 

define([ 
    'router' 
], function(
    router 
){ 
    Backbone.history.start(); 
}); 

router.js

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

     var Router = Backbone.Router.extend({ 
      routes: { 
       'scoreboard': 'scoreboardAction', 
       'game': 'gameAction', 
       '*default': 'defaultActions' 
      }, 
      defaultActions: function() { 
       scoreboard.show(); 
       scoreboard.render(); 

      }, 
      scoreboardAction: function() { 
       scoreboard.show(); 
       scoreboard.render(); 
       console.log(scoreboard.render()); 

      }, 
      gameAction: function() { 
       // TODO 
      } 
     }); 

     return new Router(); 
    }); 

scoreboard.js

define([ 
    'backbone', 
    'tmpl/scoreboard' 
], function(
    Backbone, 
    tmpl 
){ 

    var View = Backbone.View.extend({ 

     template: tmpl, 
     initialize: function() { 
      //console.log("Woohoho"); 
      //this.listenTo(this.model, "change", this.render); 
     }, 
     render: function() { 
      //console.log(this.template()); 
      //this.$el.html(this.template(this.model.toJSON())); 
      this.template(); 
      this.$el.html(this.template()); 
     }, 
     show: function() { 
      //console.log("show"); 
      this.$el.html(this.template()); 

     }, 
     hide: function() { 
      alert("Hide"); 
      // TODO 
     } 

    }); 

    return new View(); 
}); 

TMPL/scoreboard.js генерируется из шаблона if i console.log(this.template()); Я увижу действительный код html. Но в браузере я вижу nothink, whitescreen. Как я могу отобразить свой шаблон?

ответ

1

Добавьте el элемент к вашему мнению, как это:

var View = Backbone.View.extend({ 
    el: '#page', 
    .... 
+0

спасибо, это работает для меня –

Смежные вопросы