我想弄清楚为该项目建模的正确 Ember.js 方法是什么,例如。需要什么模型、路线和 Controller 。 I have started a jsBin to work from .
我的要求可以安全地减少到:
项目及其选项
仪表板
导航
数据
那么这在 Ember 中是如何构建的呢?
我曾尝试自己做一次,但这是我的第一次尝试,最后我得到了一个非常丑陋的设置。我想看看有 Ember 经验的人会如何处理这个问题:
jsBin 模型(link)
我已经创建了一系列 Handlebars 模板,但没有尝试确定应该存在哪些模型以及需要哪些 Controller ......
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"
}
}
}
}
]
}
最佳答案
我放了一个小 JSBin http://jsbin.com/IdAXuMar/5/edit
好的,在聊了聊并仔细观察之后,以下是我对如何简化它的想法:
您只有一个 URL,因此我现在只使用一个 Route 和一个 Controller。
数据模型非常简单,因为它是完全分层的:
Display 有很多Items, Item 有很多Options
而且因为您一次只看一个显示器,所以您根本不需要将显示器作为模型。如果您的应用程序不断发展并且您同时拥有多个 Display,那么实现 Display 模型并通过该模型执行所有 JSON 请求是有意义的。
我会实现一个单一的路由和 Controller :
App.Router.map(function() {
this.resource('display', path: { 'display/:id' });
});
App.DisplayRoute = Ember.Route.extend({
model: function(params) {
return App.Item.find(params._id);
}
});
DisplayController 对其所有项目具有完全访问权限,因为它们被设置为它的模型。
我认为您现在只需要一个模板,如果它变得无法控制,您可以稍后将它们拆分成多个部分。
<script type="text/x-handlebars" data-template-name="display">
{{#each model}}
<!-- access on every item here -->
{{#each option}}
{{#if isSelected}}
this option is selected
{{/if}}
<!-- access on every option here -->
<a {{action selectOption this}} href=''> Select this option</a>
{{/each}}
{{/each}}
</script>
请注意 selectOption 操作:调用此操作并传递选项时,您可以直接在选项本身上设置选定状态,这将立即反射(reflect)在 View 中。
App.DisplayController = Ember.ArrayController.extend({
// add computed properties here..
actions: {
selectOption: function(option) {
option.set('isSelected', true);
}
}
});
要从服务器获取项目,您可以调用 App.Item.find() 然后传递显示的 ID。这不是 100% 的常规,因为您应该在此处传递项目的 ID,但我认为为此目的没关系。所以这个方法看起来像
App.Item = Ember.Option.extend({
selected: false
// add computed properties here
});
App.Item.reopenClass({
// retrieves the items from the server
find: function(displayId) {
var url = "/game/json" + displayId;
var items = new Ember.A();
Ember.$.getJSON().success(function(data) {
data.items.forEach(function(jsonItem) {
var item = Ember.Item.create({
nid: jsonItem.nid,
title: jsonItem.title,
image: jsonItem.image
});
item.set('options', new Ember.A());
jsonItem.options.forEach(function(option) {
var option = Ember.Option.create({
// set option properties
});
emberItem.get('options').pushObject(option);
})
})
});
return items;
}
});
我希望这能帮助您入门,或许还能更轻松地将您的概念转移到 Ember。如果您有关于如何将所有内容保存回服务器的问题,请点击 :)
关于javascript - Ember.js:如何为这个例子建模?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19987433/
我希望将Favorite模型添加到我的User和Link模型。业务逻辑用户可以有多个链接(即可以添加多个链接)用户可以收藏多个链接(他们自己的或其他用户的)一个链接可以被多个用户收藏,但只有一个所有者我对如何为这种关联建模以及在模型就位后如何创建用户收藏夹感到困惑?classUser 最佳答案 下面的数据模型怎么样:classUser:destroyhas_many:favorite_links,:through=>:favorites,:source=>:linkendclassLink:destroyhas_many:favor
我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs
我今天看到了一个ruby代码片段。[1,2,3,4,5,6,7].inject(:+)=>28[1,2,3,4,5,6,7].inject(:*)=>5040这里的注入(inject)和之前看到的完全不一样,比如[1,2,3,4,5,6,7].inject{|sum,x|sum+x}请解释一下它是如何工作的? 最佳答案 没有魔法,符号(方法)只是可能的参数之一。这是来自文档:#enum.inject(initial,sym)=>obj#enum.inject(sym)=>obj#enum.inject(initial){|mem
我使用rails3.1+rspec和factorygirl。我对必填字段(validates_presence_of)的验证工作正常。我如何让测试将该事实用作“成功”而不是“失败”规范是:describe"Addanindustrywithnoname"docontext"Unabletocreatearecordwhenthenameisblank"dosubjectdoind=Factory.create(:industry_name_blank)endit{shouldbe_invalid}endend但是我失败了:Failures:1)Addanindustrywithnona
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我一直在玩一个脚本,它在Chrome中获取选定的文本并在Google中查找它,提供四个最佳选择,然后粘贴相关链接。它以不同的格式粘贴,具体取决于当前在Chrome中打开的页面-DokuWiki打开的DokuWiki格式,普通网站的HTML,我想要我的WordPress所见即所得编辑器的富文本。我尝试使用pbpaste-Preferrtf来查看没有其他样式的富文本链接在粘贴板上的样子,但它仍然输出纯文本。在文本编辑中保存文件并进行试验后,我想出了以下内容text=%q|{\rtf1{\field{\*\fldinst{HYPERLINK"URL"}}{\fldrsltTEXT}}}|te
我真的只是不确定这意味着什么或我应该做什么才能让网页在我的本地主机上运行。现在它只是显示一个错误,上面写着“我们很抱歉,但出了点问题。”当我运行railsserver并在chrome中打开localhost:3000时。这是控制台输出:StartedGET"/users/sign_in"for127.0.0.1at2013-07-0512:07:07-0400ProcessingbyDevise::SessionsController#newasHTMLCompleted500InternalServerErrorin55msNoMethodError(undefinedmethod`
好的,所以我有了我正在使用的应用程序的这种方法,它可以在生产中使用。我的问题为什么这行得通?这是新的Ruby语法吗?defeditload_elements(current_user)unlesscurrent_user.role?(:admin)respond_todo|format|format.json{render:json=>@user}format.xml{render:xml=>@user}format.htmlendrescueActiveRecord::RecordNotFoundrespond_to_not_found(:json,:xml,:html)end
你能解释一下吗?我想评估来自两个不同来源的值和计算。一个消息来源为我提供了以下信息(以编程方式):'a=2'第二个来源给了我这个表达式来评估:'a+3'这个有效:a=2eval'a+3'这也有效:eval'a=2;a+3'但我真正需要的是这个,但它不起作用:eval'a=2'eval'a+3'我想了解其中的区别,以及如何使最后一个选项起作用。感谢您的帮助。 最佳答案 您可以创建一个Binding,并将相同的绑定(bind)与每个eval相关联调用:1.9.3p194:008>b=binding=>#1.9.3p194:009>eva
我开始了一个新的Rails3.2.5项目,Assets管道不再工作了。CSS和Javascript文件不再编译。这是尝试生成Assets时日志的输出:StartedGET"/assets/application.css?body=1"for127.0.0.1at2012-06-1623:59:11-0700Servedasset/application.css-200OK(0ms)[2012-06-1623:59:11]ERRORNoMethodError:undefinedmethod`each'fornil:NilClass/Users/greg/.rbenv/versions/1