模型参数在{{render}}帮助程序中不起作用(Model parameter takes no effect in {{render}} helper)

编程入门 行业动态 更新时间:2024-10-18 22:31:35
模型参数在{{render}}帮助程序中不起作用(Model parameter takes no effect in {{render}} helper)

第二个参数应该是模型,但在我的示例中它不起作用:

模板:

<script type="text/x-handlebars" data-template-name="guestlist"> {{#each}} <div class="visitor-li"> <div class="user-name">{{name}}</div> <div class="clear"></div> </div> {{/each}} </script><!-- end with x-handlebars -->

JavaScript的:

window.WorkBench = Ember.Application.create({}); WorkBench.ApplicationAdapter = DS.FixtureAdapter.extend(); WorkBench.Guestlist = DS.Model.extend({ name: DS.attr("string") }); WorkBench.Guestlist.FIXTURES = [ { id: 1, name: 'aaa' }, { id: 2, name: 'bbb' }, { id: 3, name: 'ccc' } ]; WorkBench.GuestlistController = Ember.ArrayController.extend({});

HTML

<div id="con_taba_1"> {{render "guestlist" guestlist}} </div>

应该显示一个列表,但是没有去。我想知道为什么,谢谢!

The second parameter should be model, but it take no effects in my example:

template:

<script type="text/x-handlebars" data-template-name="guestlist"> {{#each}} <div class="visitor-li"> <div class="user-name">{{name}}</div> <div class="clear"></div> </div> {{/each}} </script><!-- end with x-handlebars -->

javascript:

window.WorkBench = Ember.Application.create({}); WorkBench.ApplicationAdapter = DS.FixtureAdapter.extend(); WorkBench.Guestlist = DS.Model.extend({ name: DS.attr("string") }); WorkBench.Guestlist.FIXTURES = [ { id: 1, name: 'aaa' }, { id: 2, name: 'bbb' }, { id: 3, name: 'ccc' } ]; WorkBench.GuestlistController = Ember.ArrayController.extend({});

html

<div id="con_taba_1"> {{render "guestlist" guestlist}} </div>

A list should be displayed, but no go.I want know why, thanks!

最满意答案

第二个参数实际上需要存在于您正在使用它的上下文中。 您传递的是未定义的属性。

证明这一点的一种方法就是在那里注销guestlist

<div id="con_taba_1"> {{log guestlist}} {{render "guestlist" guestlist}} </div>

guestlist属性需要存在于使用render语句的范围内,通过记录this上下文可以看到(我告诉你,但你已经遗漏了这些信息)。

<div id="con_taba_1"> {{log this}} {{log guestlist}} {{render "guestlist" guestlist}} </div>

通常,模型信息是从与上述上下文相关的route的model钩子提供的。

App.FooRoute = Em.Route.extend({ model: function(){ return { bar:'baz'}; } });

在foo模板中,你可以{{log bar}},你会看到baz 。

这是一个设置它的游乐场, http://Emberjs.jsbin.com

The second parameter needs to actually exist in the context where you're using it. You are passing in an undefined property.

A way to prove this is to just log out guestlist right there

<div id="con_taba_1"> {{log guestlist}} {{render "guestlist" guestlist}} </div>

The guestlist property needs to exist in the scope of where you are using the render statement, that context can be seen by logging this (I'd tell you, but you've left out that information).

<div id="con_taba_1"> {{log this}} {{log guestlist}} {{render "guestlist" guestlist}} </div>

generally the model information is provided from the model hook in the route related to the context mentioned above.

App.FooRoute = Em.Route.extend({ model: function(){ return { bar:'baz'}; } });

In the foo template you could do {{log bar}} and you would see baz.

Here's a playground for you for setting it up, http://Emberjs.jsbin.com

更多推荐

本文发布于:2023-07-25 05:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1256789.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   模型   参数   程序   render

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!