从ContentBinding获取内容 [英] get content in view from ContentBinding

查看:184
本文介绍了从ContentBinding获取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有新的Ember.js(1.0 pre)的问题



我得到了一些有用的Ember 0.9.8,但是当我更改这个lib的版本我注意到无法解决的问题。 (我不太在Ember:/)



这里是示例代码:
http://jsfiddle.net/ETQCc/5/



我创建视图:

 < script type =text / x-handlebarsdata-template-name =MyView> 
{{s}} / {{content.s}} / {{this.s}} / {{view.s}}
< / script>

App.myview = Em.View.extend({
layoutName:MyView,
});

我想显示一些可变的,这取决于我在内容中设置的bindig: p>

 < script type =text / x-handlebars> 
xx {{view App.myview contentBinding =App.ctrl}}
< / script>

如何在此控制器中获得变量?

解决方案

你有两种方法可以做到这一点。
您可以:




  • 将控制器绑定到视图,而不是绑定内容(请参阅 JSFiddle )。通过这样做,您将视图的上下文设置为控制器,因此 {{s}} 直接工作:



{{view App.myview controllerBinding =App.ctrl}}




  • 编辑您的模板以从视图而不是其上下文获取变量(请参阅这个JSFiddle )。通过这样做,视图内容被绑定到控制器,您的模板显示视图内容的 s 属性:



{{view.content.s}}



我建议你以查看 Ember.js查看上下文更改


I've got problem with new Ember.js (1.0 pre)

I got something what worked on Ember 0.9.8 but when I changed version of this lib I noticed problem which can't resolve. (I'm not so good at Ember :/)

Here is sample code: http://jsfiddle.net/ETQCc/5/

I created view:

<script type="text/x-handlebars" data-template-name="MyView">
    {{s}} / {{content.s}} / {{this.s}} / {{view.s}}
</script>

App.myview = Em.View.extend({
    layoutName : "MyView",
});

And I'd like to show some varible which depends on what I set in content bindig:

<script type="text/x-handlebars">
     xx{{view App.myview contentBinding="App.ctrl"}}
</script>

How to get to varible in this controller?

解决方案

You have two ways to do that. You can :

  • bind the controller to your view instead of binding the content (see the JSFiddle). By doing that you set the context of the view to be the controller, so {{s}} works directly:

{{view App.myview controllerBinding="App.ctrl"}}

  • edit your template to get the variable from the view instead of its context (see this JSFiddle). By doing that the view content is binded to the controller, and your template shows the s property of the view content:

{{view.content.s}}

I suggest you to take a look at Ember.js View Context changes.

这篇关于从ContentBinding获取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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