[英]Ember.js: How do I associate my component with a model?
I am new to Ember.js and am trying to figure out how to piece things together at this point. 我是Ember.js的新手,正在尝试弄清楚如何将这些东西组合在一起。 One component I built, since I need a re-usable "widget" to use across many portions of my application, is a "site nav" widget.
由于我需要在应用程序的许多部分中使用可重用的“窗口小部件”,因此构建的一个组件是“站点导航”窗口小部件。 Basically, it's almost like the buttons/links you see in StackOverflow when you open a new question titled: "Questions Tags Users Badges Unanswered".
基本上,这就像打开一个新问题时在StackOverflow中看到的按钮/链接一样:“问题标记用户未回答的徽章”。 In my app, these links have a name and id associated with them on the server-side.
在我的应用程序中,这些链接在服务器端具有与之关联的名称和ID。 I want to be able to use this navigation widget on multiple parts of my app and it should be as simple as putting:
我希望能够在我的应用程序的多个部分上使用此导航小部件,并且应该像放置以下内容一样简单:
{{site-nav}}
into a template. 进入模板。 I got that part working just fine, but the navigation is currently hard coded in handlebars.
我的那部分工作正常,但导航当前已在车把中进行了硬编码。 My question is, for a component, where is the right place to retrieve/populate model data from the server?
我的问题是,对于组件而言,从服务器检索/填充模型数据的正确位置在哪里? For a controller, we do it directly from the controller's route definition.
对于控制器,我们直接从控制器的路由定义中进行操作。 The component is not associated with a router.
该组件未与路由器关联。 In fact, it can be re-used, as mentioned before, in several parts of the app.
实际上,如前所述,它可以在应用程序的多个部分中重复使用。
I want to be able to drop this component into templates and have it populated with modeled nav from the server which has the name/IDs of the navigation I need. 我希望能够将此组件放到模板中,并用服务器中具有导航所需名称/ ID的模型导航来填充它。 Where is the best place to do this?
最好的地方在哪里? I'm guessing I'll still extend from something like
DS.Model
, but I'm not entirely sure where/when/how to integrate this with the component. 我猜我仍然会从
DS.Model
东西扩展,但是我不确定是在哪里/何时/如何将其与组件集成。 When do I create the model and invoke a .find()
type call to the server to populate site-nav
with data? 什么时候创建模型并调用
.find()
类型的服务器调用以用数据填充site-nav
?
you can pass value to component Passing properties to component via handlebars. 您可以将值传递给组件通过把手将属性传递给组件 。
{{my-nav navlist=listfromserver}}
so the list from server is in our controller can be passed to the component 因此服务器中的列表位于我们的控制器中,可以传递给组件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.