简体   繁体   English

绑定attr手把助手ember.js与特定值检查

[英]Bind attr handlebars helper ember.js with a specific value check

So I basically need to select a specific value from the selectbox. 因此,我基本上需要从选择框中选择一个特定的值。

So I am wondering how I can make {{bind-attr}} behave like this: 所以我想知道如何使{{bind-attr}}表现如下:

        <select id="research_status" class="form-control">
            <option value=""></option>
            <option value="finalized" {{bind-attr selected a_variable="finalized"}}>Finalized</option>
            <option value="in progress" {{bind-attr selected a_variable="in progress"}}>In progress</option>
        </select>

I couldn't find this in the documentation (I haven't looked into it too much though). 我在文档中找不到这个(虽然我没有对此进行过多研究)。 But if there is another way of doing this that's a little simpler or maybe easier to work with please let me know. 但是,如果还有另一种方法可以使工作更简单或更容易,请告诉我。

I do not want to endup doing something like this 我不想做这样的事情

        <select id="research_status" class="form-control">
            <option value=""></option>
            <option value="finalized" {{bind-attr selected=isFinalized}}>Finalized</option>
            <option value="in progress" {{bind-attr selected=isInProgress}}>In progress</option>
        </select>

I know we can do it like this, but I do not want to go around creating unncessary variables. 我知道我们可以这样做,但是我不想四处创建不必要的变量。

I am retrieving this from a model so it'd be great to just check the value of that model's field and select that specific one in the select box (as shown in my first snippet of code). 我正在从模型中检索此数据,因此最好检查该模型字段的值,然后在选择框中选择该特定字段(如我的第一段代码所示)。

EDIT: I forgot how Handlebars helpers handle attributes. 编辑:我忘记了Handlebars助手如何处理属性。 So my previous answer wouldn't work. 所以我以前的答案行不通。 But the bind-attr helper doesn't provide that kind of functionality, and is probably different enough where it can't be easily modified to do so. 但是bind-attr助手没有提供这种功能,在可能不容易修改的地方可能足够不同。 I believe your best bet would be to re-create the bind-attr helper, only using your custom syntax. 我相信最好的选择是仅使用自定义语法重新创建bind-attr帮助器。 You can find that code here . 您可以在此处找到该代码。

Again, I think you'd be better off just declaring a controller property. 同样,我认为您最好声明一个控制器属性。 It only takes one line per property: 每个属性只需要一行:

App.MyController = Ember.Controller.extend({
    isFinalized: Ember.computed.equal('model.state', 'finalized'),
    isInProgress: Ember.computed.equal('model.state', 'in progress')
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM