Backbone View渲染问题

时间:2014-09-26 00:29:50

标签: backbone.js requirejs

虽然我搜索过但找不到答案。欢迎任何重复的链接。

我正在尝试从html模板渲染骨干视图。 我正在使用 require.js 及其 text.js 插件。 虽然我可以收集模板并将其传递给我在回调点main.js要求我的视图时使用的回调; 我无法渲染它来填充index_app.html中所需的html元素 文件,我想从我的模板中放置文本的地方是#form-login

我错过了什么?它与我的dom选择器有关吗?

这是它的样子。

index_app.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>

    <!-- Sets initial viewport load and disables zooming  -->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">

    <!-- Makes your prototype chrome-less once bookmarked to your phone's home screen -->
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">

    <!-- Include the compiled Ratchet CSS -->
    <link href="../lib/ratchet/css/ratchet.css" rel="stylesheet">

    <script data-main="main" src="../lib/require/require.js"></script>

  </head>
  <body>
    <!-- Wrap all non-bar HTML in the .content div (this is actually what scrolls) -->
    <div class="content" id="cont">
        <div id="form_login">
        </div>
    </div>

  </body>
</html>

main.js:

require.config({
 paths: {
        backbone: '../lib/backbone/backbone-min',
        jquery: '../lib/jquery/jquery-1.11.1.min',
        ratchet: '../lib/ratchet/js/ratchet',
        text: '../lib/require/text',
        underscore: '../lib/underscore/underscore-min',
 },
 shim: {
 'backbone': {
 deps: ['underscore', 'jquery'],
 exports: 'Backbone'
 },

 'underscore': {
 exports: '_'
 },
 }
});


require(['views/login'], function(LoginView){
 var loginView = new LoginView();
});

/视图/登录:

define([
  'jquery',
  'underscore',
  'backbone',
  'text!templates/login.html'
  ], function($, _, Backbone, LoginTmpl){
      var LoginView = new Backbone.View.extend({

          el: $("#cont"),

          template: _.template(LoginTmpl),

          initialize: function(){
              this.render();
          },

          render: function() {
            this.$("#form_login").html(this.template);
            return this;
          }
      });
    //alert(LoginTmpl);

      return LoginView;
});

/templates/login.html:

<div class="card">
    <form method="POST" action="/login">
        <input type="text" placeholder="Username" name="username">
        <input type="password" placeholder="Password" name="password">
        <ul class="table-view">
            <li class="table-view-cell">
                Remember me
                <div class="toggle active">
                    <div class="toggle-handle"></div>
                </div>
            </li>
        </ul>
        <button class="btn btn-primary btn-block">Login</button>
    </form>
</div>

我的目录树:

/site
    -/lib/
      ...
    -/src/
         -index_app.html
         -main.js
         -collections/
         -views/
            -login.js
         -templates/
            -login.html
         -models/

1 个答案:

答案 0 :(得分:0)

在你的&#34; / views / login&#34;

var LoginView = new Backbone.View.extend({

应该是

var LoginView = Backbone.View.extend({

(没有new