物业在初始化模板无法访问访问? [英] Property accessible in initialize not accessible in template?

查看:170
本文介绍了物业在初始化模板无法访问访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当实例化一个新的观点,我传递一个典范。我可以访问该模型中的初始化属性,但我不能引用它,当我试图通过模型转换成一个模板。任何想法,为什么?

  VAR后览= Backbone.View.extend({        初始化:功能(){           //返回模型
            的console.log('模式,我们有兴趣,this.model);
            this.render();
        },        EL:#博客,发帖,        模板:功能(){            //返回undefined
            VAR模型= this.model;            返回_.template($('#后览)。HTML(){
                岗位:模型
            });
        },        渲染:功能(){
            VAR自我=这一点;
            这$ el.html(self.template);
        }    });

我在另一个视图中使用的方法实例化:

  readMore:函数(即指数){
            VAR自我=这一点;
            VAR NewView的=新后览({
                型号:self.collection.models [指数] .toJSON()
            });        }


解决方案

你传递一个函数这$ el.html

 这$ el.html(self.template)。

这是相同的话说:

 变种F = this.template;
这$ el.html(F);

那么,是什么 HTML 你当你传递一个函数?那么从​​精细的手工


  

的.html(功能)


  
  

一个函数返回的HTML内容进行设置。收到指数
  在该组中的元素和旧HTML值作为位置
  参数。 jQuery的清空调用函数之前的元素;使用
  在oldhtml参数引用previous内容。内
  功能,这个是指在设定的当前元素。


当你通过 HTML 函数,它会调用该函数,但这个不会是你在想什么它是,这个的功能将是有其一套HTML DOM元素里面。

我想你想叫 this.template 自己和它的返回值交给 HTML

 这$ el.html(this.template());
// ------------------------ ^^

这样模板将有观为这个如你期望它。

When instantiating a new view, I'm passing in a model. I have access to that model in the "initialize" property, but I can't reference it when I'm trying pass the model into a template. Any idea why?

var postView = Backbone.View.extend({

        initialize : function() {

           // returns model 
            console.log('the model we are interested in',this.model); 
            this.render();
        },

        el : "#blog-post",

        template : function() {

            // returns undefined
            var model = this.model;

            return _.template($('#postview').html(), {
                post : model
            });
        },

        render : function() {
            var self = this;
            this.$el.html(self.template);
        }

    });

I'm instantiating it using a method in another view:

readMore : function(e, index) {
            var self = this;
            var newView = new postView({
                model : self.collection.models[index].toJSON()
            });

        }

解决方案

You're passing a function to this.$el.html:

this.$el.html(self.template);

That's the same as saying:

var f = this.template;
this.$el.html(f);

So what does html do when you pass it a function? Well, from the fine manual:

.html( function )

A function returning the HTML content to set. Receives the index position of the element in the set and the old HTML value as arguments. jQuery empties the element before calling the function; use the oldhtml argument to reference the previous content. Within the function, this refers to the current element in the set.

When you pass html a function, it will call that function but this won't be what you think it is, this inside the function will be the DOM element that is having its HTML set.

I think you want to call this.template yourself and hand its return value to html:

this.$el.html(this.template());
// ------------------------^^

That way template will have the view as its this as you expect it to.

这篇关于物业在初始化模板无法访问访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆