本人原文地址发布在:点击这里

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:

 var Person = DS.Model.extend({
firstName: DS.attr('string'),
birthday: DS.attr('date')
});

And we used like this:

 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:

 // app/controllers/

 import DS from ‘ember-data’;

 import Ember from ‘ember’;

 modelName: “info”,

 init: function() {

   var info = this.getStructureInfo();

   this.container.register('model:' + this.get(‘modelName’),       this.generateModel(info));

 },

 generateModel: function(content) {

 var attrs =  {};

 for(var key in content) {

 attrs[key] = DS.attr(‘string’);

 }

 },

 getStructureInfo: function() {

 return [‘car', ‘price', ‘discount’];

 }

In Step 1, we get structure information from “getStructureInfo” function, and then register a “info” model created by “generateModel” function.

Step 2:

 // app/controllers/
loadData: function() {
return this.store.find(this.get(‘modelName’);
}

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)

Ember.js之动态创建模型的更多相关文章

  1. js如何动态创建表格(两种方法)

    js如何动态创建表格(两种方法) 一.总结 一句话总结: 1.方法一:写好创建表格的html代码,将之赋值给div的innerHTML. 2.方法二.直接用创建好的table元素的方法insertRo ...

  2. JS动态引入js,CSS——动态创建script/link/style标签

    一.动态创建link方式 我们可以使用link的方式.如下代码所示. 二.动态创建style方式 但是,这样的话,需要加载整个css文件,但是那样有可能浪费一个http请求并占用一个服务器请求数,并等 ...

  3. js/jq动态创建表格的行与列

    之前做了一个项目,需求是能动态创建表格行,动态创建表格的列,度了很多资料,都没有动态创建列的插件,所以自己动手写了一个 需求大概是(下图) 1.动态添加一行.2.动态添加一列,3.删除行.4.删除列, ...

  4. JS面向对象——动态原型模型、寄生构造模型

    动态原型模型 组合使用构造函数模型和原型模型,使得OO语言程序员在看到独立的构造函数和原型时很困惑.动态原型模型致力于解决该问题,它把所有的信息封装在构造函数中,通过在构造函数中初始化原型(仅在必要情 ...

  5. [EF] - 动态创建模型:System.Reflection.Emit + Code First

    动态创建Entity Framework模型并且创建数据库 使用System.Reflection.Emit+Code First model创建以下的一个实体类和DbContext并且创建数据库: ...

  6. js原生动态创建表格

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. JS/JQ动态创建(添加)optgroup和option属性

    JavaScript和Jquery动态操作select下拉框 相信在前端设计中必然不会少的了表单,因为经常会使用到下拉框选项,又或是把数据动态回显到下拉框中.因为之前牵扯到optgroup标签时遇到了 ...

  8. js的传值,table中tr的遍历,js中动态创建数组

    1.这里关键是对页面中的传值,其次是动态的创建一个数组,用来存值 $(val).css("background-color", "rgb(251, 248, 233)&q ...

  9. js···DOM2动态创建节点

    1.生成节点的方法  document.createElement(“div”) 2.插入节点的方法   父元素.appendChild(新节点) 在父节点中的子节点后面插入新的节点 3.在指定的位置 ...

随机推荐

  1. ng-init,ng-controller,ng-model

    1.ng-init 用于初始化数据,跟在$scope插入数据一样,但是在配合repeat指令时候比较有用: <div ng-repeat="arrOuter in arr" ...

  2. 腾讯OAuth授权联合登录

    /** * unionLoginCallbackPath */ @Value("${QQ_UNION_LOGIN_CALLBACK_PATH}") private String q ...

  3. vSphere6提示已弃用VMFS卷的解决方法

    Deprecated VMFS volume(s) found on the host. Please consider upgrading volume(s) to the latest versi ...

  4. rsync排除文件同步

    排除扩展名为log的文件 rsync -ave ssh --exclude '*.log' root@192.168.168.188:/website/abc.com/* /website/abc.c ...

  5. Groovy 模版引擎

    1. Introduction Groovy supports multiple ways to generate text dynamically including GStrings, print ...

  6. Git入门仅这篇就够了

    版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:http://www.cnblogs.com/cavalier-/p/5978937.html 前言 大家好,我是Cavalier ...

  7. 用Backbone.js创建一个联系人管理系统(四)

    原文: Build a Contacts Manager Using Backbone.js: Part 4 这一系列教程的第四部分,教我们如何完成对已经存在的Contacts进行编辑和保存. 本教程 ...

  8. SharePoint 2013异常信息的查看

    刚刚学习SharePoint开发的时候,经常遇到一些异常,却不能直接看到详细信息,很郁闷.这里做下简单的整理,方便查找: 1.代码未处理异常出现黄页——”‘/’应用程序中的服务器错误.运行时错误“. ...

  9. liunx 防火墙开放端口的设置

    今天在liunx 服务器上遇到一个问题,tomcat服务启动后怎么也访问不到项目,找了好久的原因,终于发现原来是liunx服务防火墙限制服务端口的访问,也就不多说了,看下面解决方法. 1.查看防火墙的 ...

  10. webDriver环境搭建与测试

    1.安装jdk 2.安装eclipse 3.安装selenium 由于使用的是开发语言是java,因此需要安装java版的selenium包.下载地址:http://pan.baidu.com/s/1 ...