简体   繁体   中英

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:

        <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. 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. I believe your best bet would be to re-create the bind-attr helper, only using your custom syntax. 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')
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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