5

我已经完成了与 Chosen 的示例 Ember.js 集成(https://github.com/harvesthq/chosen)

咖啡脚本:

App.ChosenSelectView = Em.Select.extend({
  didInsertElement: ->
    @_super()
    @$().chosen()
    # Assumes optionLabelPath is something like "content.name"
    @addObserver(@get("optionLabelPath").replace(/^content/, "content.@each"), ->  @contentDidChange())
  contentDidChange: ->
    # 2 ticks until DOM update
    Em.run.next(this, (-> Em.run.next(this, (-> @$().trigger("liszt:updated")))))
})

困扰我的事情是我不知道在触发选择小部件上的更新之前我需要多少时间。根据我的实验,2 个运行循环是可以的,但也许有更好的方法来处理整个事情?

jsfiddle 的完整示例:http: //jsfiddle.net/oruen/qfYPy/

4

3 回答 3

7

我认为问题在于您的观察者被通知得太早了,这意味着更改尚未写入 DOM。

我已经破解了一点,最后我想出了一个解决方案,它在触发插件Ember.run.sync()事件之前调用,请参阅http://jsfiddle.net/pangratz666/dbHJb/chosen

车把

<script type="text/x-handlebars" data-template-name="selectTmpl" >
    {{#each items tagName="select" }}
        <option {{bindAttr value="id"}} >{{name}}</option>    
    {{/each}}
</script>​

JavaScript

App = Ember.Application.create({});

App.items = Ember.ArrayProxy.create({
    content: [Ember.Object.create({
        id: 1,
        name: 'First item'
    }), Ember.Object.create({
        id: 2,
        name: 'Second Item'
    })]
});

Ember.View.create({
    templateName: 'selectTmpl',
    itemsBinding: 'App.items',

    didInsertElement: function() {
        this.$('select').chosen();
    },

    itemsChanged: function() {
        // flush the RunLoop so changes are written to DOM?
        Ember.run.sync();
        // trigger the 'liszt:updated'
        Ember.run.next(this, function() {
            this.$('select').trigger('liszt:updated');
        });
    }.observes('items.@each.name')
}).append();

Ember.run.later(function() {
    // always use Ember.js methods to acces properties, so it should be 
    // `App.items.objectAt(0)` instead of `App.items.content[0]`
    App.items.objectAt(0).set('name', '1st Item');
}, 1000);​
于 2012-04-02T18:29:31.080 回答
4

Michael Grosser 在这里发布了他的 ChosenSelectView:http: //grosser.it/2012/05/05/a-chosen-js-select-filled-via-ember-js

于 2012-05-05T10:48:24.257 回答
0

这可能适用于 Ember 1.0 和 Chosen v0.12:

JavaScript:

App.ChosenSelect = Ember.Select.extend({
   chosenOptions: {width:'100%', placeholder_text_multiple: 'Select Editors', search_contains: true},
   multiple:true,
   attributeBindings:['multiple'],

   didInsertElement: function(){
    var view = this;
    this._super();
    view.$().chosen(view.get('chosenOptions'));

    // observes for new changes on options to trigger an update on Chosen 
    return this.addObserver(this.get("optionLabelPath").replace(/^content/, "content.@each"), function() {
      return this.rerenderChosen();
    });

  },
  _closeChosen: function(){
    // trigger escape to close chosen
    this.$().next('.chzn-container-active').find('input').trigger({type:'keyup', which:27});
  },

 rerenderChosen: function() {
    this.$().trigger('chosen:updated');
  }
});

车把:

{{view App.ChosenSelect
    contentBinding="options"
    valueBinding="selectedOption"
    optionLabelPath="content.name"
}}
于 2013-09-10T06:21:25.287 回答