骨干视图脚本错误

时间:2016-10-23 05:12:08

标签: javascript backbone.js

为什么我在这里收到脚本错误?

最初我想问的是在视图初始化期间传递一个自定义变量,但我接下来了。

JSFiddle这个

var ddd = Backbone.View.extend({
  el: "#sd",
  initialize: function() {
    console.log("rrr");
  },
  render: function() {

  }
});


console.log("dd");
var t = new ddd();
console.log(t);
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.1.7/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>

<div id="sd">
  <p id="eee">

  </p>
</div>

2 个答案:

答案 0 :(得分:1)

如果您想将自定义变量传递到骨干视图,请执行以下操作。

在您的视图中

初始化

var err = 'somthing';
var t = new ddd({err: err})

当您创建视图的新实例时,只需将变量作为参数传递给

制作新的View实例

<html>
<head> 
    <script   src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
</head>
<body>
    <div id="sd">
        <p id="eee">

        </p>
    </div>
    <script>
        var PN = Backbone.View.extend({
            el: $('#sd'),
            initialize: function(options) {
                    this.eee = options.eee
            },
            render: function() {
                 console.log(this.eee);
                    return this;
            }
        });

        var v = new PN({eee: 'somthing'});
        v.render();
    </script>
</body>
</html>

修正版

您的代码应如下所示

$this->Session->write('Form1.field1', $value);

答案 1 :(得分:1)

由于您使用的是较旧版本的1.1.7版本的underscore.js。 Backbone 1.3.3使用更高版本的underscore.js。因为backbone对underscore.js有依赖性。希望这有帮助

var ddd = Backbone.View.extend({
  el: "#sd",
  initialize: function() {
    console.log("rrr");
  },
  render: function() {

  }
});


console.log("dd");
var t = new ddd();
console.log(t);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>

<div id="sd">
  <p id="eee">

  </p>
</div>