根据Ember中的类型使用不同的原型

时间:2015-11-28 14:11:57

标签: javascript ember.js

我和Ember.js atm一起玩,到目前为止,我还没有真正习惯这个概念。我现在要做的是构建一个小cms,我需要类似于Class或Object的内容,具体取决于内容的类型。例如。我加载了一个text类型的元素,这个类型有一些内容。另一个元素属于Section类型,它具有属性大小,颜色等等。我现在需要的是让路线决定它加载了什么样的内容,并依赖于特殊属性的新构造。我们说

Ember Route loads content, looks up type
  if type == text
    make a new text-oject/class with properties name, heading, content

  if type==section
    make a new section-object/class with properties color, size etc.

如何在Ember中实现这一点,以便为每种内容类型设置某种原型,并从不同的db-relationship中加载使用过的属性?

修改 好的,我试着更好地解释一下。我有一个包含三个不同关系'元素','内容'和'属性'的数据库。现在我有一个获取元素id的路由。有了这个id,我可以得到元素本身,它有字段'type'。我想做的(如果可能的话)是:

model(params) {
  element=this.store.queryRecord('element', {id: params.id});
  if (element.type == section) {
    return new sectionElement(params.id)
  }
}

SectionElement {
  construct(id) {
    element: this.store.queryRecord('element', id),
    size: this.store.query('attribute', foreignKey=id),
  }
  update() {
    update element and size
  }
}

TextElement {
  construct(id) {
    element: this.store.queryRecord('element', id),
    text: this.store.query('content', foreignKey=id),
  }
  update() {
    update element and text
  }
}

actions: {
  newElement(type) {
    if (type==section) {
      return new SectionElement 
    }
  }
}

我知道我不能在模型中使用Promises,但也许我可以理解我想做什么。正如我之前尝试解释的那样,我需要一些原型,它知道哪些值很重要以及在哪里存储它们。我仍然不确定Ember是否可以这样做,或者我是否需要以另一种方式工作。

0 个答案:

没有答案