首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据模型选择视图类型

根据模型选择视图类型
EN

Stack Overflow用户
提问于 2013-04-23 13:27:51
回答 2查看 1.1K关注 0票数 3

我有一个我想和Ember一起展示的物品清单。对于这些项目中的每一项,我都希望能够基于每个模型中的"message_type“字段动态选择用于显示它的视图类型。

我目前有类似这样的东西,它非常糟糕,并且不可伸缩:

代码语言:javascript
复制
{{#each message in controller}}

  {{#if message.isImage}}
    {{view App.ImageMessageView}}
  {{/if}}

  .... 

  {{#if message.isVideo}}
    {{view App.VideoMessageView}}
  {{/if}}

{{/each}}

如何根据Ember中的模型字段动态选择视图?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-23 20:50:58

下面是一个类似的问题,它展示了两种方法:Collection of objects of multiple models as the iterable content in a template in Ember.js

基于属性或其类型呈现项

我知道有两种方法:

  1. 为每个对象添加一个布尔属性,并使用handlebars {{#if}}检查该属性并呈现正确视图
  2. extend Ember.View,并使用计算属性来切换根据呈现的对象类型(基于Select view template by model type/object value using Ember.js)

)呈现哪个模板

方法1

JS:

代码语言:javascript
复制
App.Post = Ember.Object.extend({
  isPost: true
});

App.Bookmark = Ember.Object.extend({
  isBookmark: true
});

App.Photo = Ember.Object.extend({
  isPhoto: true
});

模板:

代码语言:javascript
复制
<ul>
  {{#each item in controller.stream}}
      {{#if item.isPost}}
        <li>post: {{item.name}} {{item.publishtime}}</li>
      {{/if}}
      {{#if item.isBookmark}}
        <li>bookmark: {{item.name}} {{item.publishtime}}</li>
      {{/if}}
      {{#if item.isPhoto}}
        <li>photo: {{item.name}} {{item.publishtime}}</li>
      {{/if}}
  {{/each}}
   </ul>

方法2

JS:

代码语言:javascript
复制
App.StreamItemView = Ember.View.extend({
  tagName: "li",
  templateName: function() {
    var content = this.get('content');
    if (content instanceof App.Post) {
      return "StreamItemPost";
    } else if (content instanceof App.Bookmark) {
      return "StreamItemBookmark";
    } else if (content instanceof App.Photo) {
      return "StreamItemPhoto";
    }
  }.property(),

  _templateChanged: function() {
        this.rerender();
    }.observes('templateName')
})

模板:

代码语言:javascript
复制
<ul>
{{#each item in controller.streamSorted}}
    {{view App.StreamItemView contentBinding=item}}
{{/each}}
 </ul>

JSBin example -未排序列表使用方法1呈现,已排序列表使用方法2呈现

票数 7
EN

Stack Overflow用户

发布于 2013-04-23 15:18:01

这可能需要更多的思考,但这是我快速想到的:

代码语言:javascript
复制
var get = Ember.get,
    isGlobalPath = Ember.isGlobalPath,
    normalizePath = Ember.Handlebars.normalizePath;

var getProp = function (context, property, options) {
    if (isGlobalPath(property)) {
        return get(property);
    } else {
        var path = normalizePath(context, property, options.data);
        return get(path.root, path.path);
    }
};

Ember.Handlebars.registerHelper('detect', function (definition, instance, options) {
    Ember.assert("You must pass exactly two argument to the detect helper", arguments.length === 3);
    Ember.assert("You must pass a block to the detect helper", options.fn && options.fn !== Handlebars.VM.noop);

    var path = '_detect_' + definition.replace('.', '_').toLowerCase();
    context = (options.contexts && options.contexts[0]) || this;
    definition = getProp(context, definition, options);
    instance = getProp(context, instance, options);
    context.set(path, definition.detectInstance(instance));

    return Ember.Handlebars.helpers.boundIf.call(options.contexts[0], path, options);
});

然后,您可以像这样使用帮助器:

代码语言:javascript
复制
{{#detect App.Definition instance}}
    DETECTED
{{else}}
    NOT DETECTED
{{/detect}}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16161614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档