Ember.js之动态创建模型
本人原文地址发布在:点击这里
What problem did we meet?
As ember document suggestion, we may define a model as a structure which will be used for serialization or deserialization to request from RESTful-based server or others.
if we define a model as:
1 var Person = DS.Model.extend({ 2 firstName: DS.attr('string'), 3 birthday: DS.attr('date') 4 });
And we used like this:
1 this.store.find('person', 1); // => { id: 1, name: 'steve-buscemi' }
At the last, the “Store” will give us an object which has been deserialised by “Adapter” in Ember.js, it just like Object-relationship mapping(ORM), we could use an object instead of json string, it’s very convenient.
It must have a premise that we have known what json string is or a constant structure. But if not,how do we solve this problem in Ember.js?
How did we solve this problem?
The problem is a common issue about “how to create an object/model dynamically”, it is difficult maybe, if you have some experience with C# or Java language, because they are both static type of language which is difficult to create or define a structure at runtime. But in javascript you can easily define a object whatever you want, it’s dynamic type of language also in Ember.js, we just need to do little work to implement this feature.
I will give an example as below:
Step 1:
1 // app/controllers/ 2 3 import DS from ‘ember-data’; 4 5 import Ember from ‘ember’; 6 7 8 modelName: “info”, 9 10 11 init: function() { 12 13 var info = this.getStructureInfo(); 14 15 this.container.register('model:' + this.get(‘modelName’), this.generateModel(info)); 16 17 }, 18 19 20 generateModel: function(content) { 21 22 var attrs = {}; 23 24 for(var key in content) { 25 26 attrs[key] = DS.attr(‘string’); 27 28 } 29 30 }, 31 32 33 getStructureInfo: function() { 34 35 return [‘car', ‘price', ‘discount’]; 36 37 }
In Step 1, we get structure information from “getStructureInfo” function, and then register a “info” model created by “generateModel” function.
Step 2:
1 // app/controllers/ 2 loadData: function() { 3 return this.store.find(this.get(‘modelName’); 4 }
In step 2, we just use “loadData” function to load data from store automatically, the only one parameter is modelName. Is it easy?
In Ember.js the container.register function could inject object on runtime, so we could inject model or other object into “Store”, then we used the “injected” object directly.
Reference
[ORM](https://en.wikipedia.org/wiki/Object-relational_mapping)
[static vs dynamic language](http://stackoverflow.com/questions/1517582/what-is-the-difference-between-statically-typed-and-dynamically-typed-languages)
作者:Stephen Cui
出处:http://www.cnblogs.com/cuiyansong
版权声明:文章属于本人及博客园共有,凡是没有标注[转载]的,请在文章末尾加入我的博客地址。
如果您觉得文章写的还不错,请点击“推荐一下”,谢谢。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?