Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在主干网中调用超类方法_Javascript_Backbone.js - Fatal编程技术网

Javascript 在主干网中调用超类方法

Javascript 在主干网中调用超类方法,javascript,backbone.js,Javascript,Backbone.js,为什么上面的代码不起作用?为什么我不能在DerivedView中调用someFunction?有没有办法做到这一点 我正在使用最新版本。执行此操作时: var BaseView = Backbone.View.extend({ localizedTemplate : function (element) { template = _.template(element.html()); return function (data) {

为什么上面的代码不起作用?为什么我不能在DerivedView中调用someFunction?有没有办法做到这一点

我正在使用最新版本。

执行此操作时:

var BaseView = Backbone.View.extend({
    localizedTemplate : function (element) {
        template = _.template(element.html());

        return function (data) {
            return template($.extend({}, data, resource));
        };
    }
});

var DerivedView = BaseView.extend({
   initialize: function (options) {
       this.model = options.model;

       this.template = function () {
         return this.localizedTemplate($("#someTemplate"));
       };
   },

   render: function () {
       var output = this.template(this.model.toJSON());
       this.$el.append(output);
       return this;
   }
});
var DerivedView = BaseView.extend({
   someVariable: function(someData) {
      return this.someFunction(someData);
   }
});
您正在将函数分配给此.template。请注意,
localizedTemplate
还返回一个函数:

this.template = function () {
    return this.localizedTemplate($("#someTemplate"));
};
var output = this.template(this.model.toJSON());
这意味着
this.template
是一个返回函数的函数,第二个函数是希望
this.model.toJSON()
作为参数的函数

您正在这样做:

return function (data) {
    return template($.extend({}, data, resource));
};
this.template
中的函数忽略其参数并返回一个函数,该函数将留给您以下函数:

this.template = function () {
    return this.localizedTemplate($("#someTemplate"));
};
var output = this.template(this.model.toJSON());
输出中
。您可能认为,
output
此时是一块HTML,因此将其交给
append

function () {
    return this.localizedTemplate($("#someTemplate"));
}
但是,
output
是一个函数,它的参数是什么?jQuery调用该函数的方式如下:

函数(索引,html)
类型:函数()
返回HTML字符串、DOM元素或jQuery对象以在匹配元素集中的每个元素末尾插入的函数。接收集合中元素的索引位置和元素的旧HTML值作为参数。在函数中,
引用集合中的当前元素

因此,
output
函数将由jQuery的
append
调用,
append
将提供编译模板函数不理解的参数。结果是一大堆混乱

如果您真的想做这样的事情,那么您需要自己调用所有函数,以便能够在正确的位置获得正确的参数:

this.$el.append(output);
演示:

或者更好的是,根本不用担心所有额外的包装。在视图的
初始化
中这样说:

var output = this.template()(this.model.toJSON());
// -----------------------^^
然后在
渲染中执行以下操作:

this.template = this.localizedTemplate($("#someTemplate"));
演示:

还请注意,您不需要
this.model=options.model
,它将为您执行以下操作:

有几个特殊选项,如果传递,将直接附加到视图:
模型
集合
el
id
类名
标记名
属性


或者干脆
someVariable:this.someFunction(someData)someVariable
不应该是一个方法,而是一个值。是的,但我认为在大多数情况下,至少在
view.initialize
之前,您不会有
someData
。更新了代码,但它仍然不起作用。请查看新代码并给出您的建议。在
localizedTemplate
中,您需要在
模板
变量前添加
。对于您现在拥有的,
template
是一个全局变量。var BaseView=Backbone.View.extend({localizedTemplate:function(element){self=this;this.template=uu.template(element.html());return function(data){return self.template($.extend({},data,resource));};});仍然不工作…:(