dou*_*ack 8 javascript architecture ember.js
我试图找出正确的Ember.js模型这个项目的方法,例如.需要什么型号,路线和控制器. 我已经开始使用jsBin了.
我的要求可以安全地降低到:
物品及其选择
仪表板
导航
数据
那么在Ember中如何构建呢?
我曾尝试过一次这样做,但这是我的第一次尝试,最后我得到了一个非常丑陋的设置.我想看看Ember经验的人会如何接近这个:
jsBin Mockup(链接)
我已经创建了一系列车把模板,但没有尝试应该存在哪些型号以及需要哪些控制器.
JSON
{
"Items" : [
{
"Item" : {
"nid" : "3",
"title" : "Hydro",
"image" : "http://bpf.vm/sites/default/files/bpf_things/hydro.jpg",
"properties" : "Baseload, Intermittent",
"values" : {
"Cost" : {
"price" : "6",
"quantity" : null
},
"Percent of Portfolio" : {
"price" : null,
"quantity" : "56"
}
},
"options" : {
"1" : {
"price" : "1512",
"quantity" : "10000"
},
"12" : {
"price" : "825",
"quantity" : "20000"
},
"11" : {
"price" : "550",
"quantity" : "50000"
}
}
}
},
{
"Item" : {
"nid" : "4",
"title" : "Nuclear",
"image" : "http://bpf.vm/sites/default/files/bpf_things/nuclear.jpg",
"id" : "",
"properties" : "Baseload, Predictable",
"values" : {
"Cost" : {
"price" : "8",
"quantity" : null
},
"Percent of Portfolio" : {
"price" : null,
"quantity" : "21"
}
},
"options" : {
"4" : {
"price" : "825",
"quantity" : "10000"
},
"13" : {
"price" : "411",
"quantity" : "15000"
}
}
}
},
{
"Item" : {
"nid" : "5",
"title" : "Natural Gas",
"image" : "http://bpf.vm/sites/default/files/bpf_things/gas.jpg",
"id" : "9",
"properties" : "Baseload, Predictable",
"values" : {
"Cost" : {
"price" : "5",
"quantity" : null
},
"Percent of Portfolio" : {
"price" : null,
"quantity" : "24"
}
},
"options" : {
"7" : {
"price" : "400",
"quantity" : "50000"
},
"10" : {
"price" : "600",
"quantity" : "100000"
}
}
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
这是答案的开头:
楷模
我想我这里只需要三个模型。仪表板是该应用程序的主要参与者,但它没有自己的任何数据。
控制器
早些时候我完全忽略了 ArrayController 的概念。一般来说,任何集合都需要一个 ArrayController 来表示它,而不是一个普通的 ember ObjectController。我的“项目”将需要一个,但我认为“选项”不需要,因为选项是项目的子项,并且可以使用项目/项目作为代理。
模板
这里的缩进代表渲染其他模板的模板。例如,我的显示模板包含{{render dashboard}}
和{{render items}}
。
路线
这还是很朦胧的。路由似乎扮演着很多角色(将 url 映射到模型、为控制器设置模型,也许还有其他东西??)。目前我能想到的唯一需要的网址是:
其他路线:
setupController
:将控制器设置为空白/保存显示redirect
:只是重定向到显示路由(本质上是应用程序的根目录)model
:将给定的显示器设置为模型afterModel
:加载显示指定的项目我想仅此而已。这是一个简单的应用程序,一旦我为显示加载了项目,那么该应用程序只会更改屏幕的显示。有用户选择,但它们是布尔标志(例如,在项目上设置 isSelected 应该更改仪表板显示的数据) - 这些选择不需要任何导航。