Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js and jQuery Sortable. How to work around the metamorph scripts

I have an ember.js app that I would like to use jquery ui's sortable widget on. My view looks like

<ul id="sort-container">
{{#each content}}
    <li>{{title}}</li>
{{/each}}
</ul>

Sorting works fine, until one of the bindings needs to update. The problem is that each <li> gets surrounded by ember's infamous metamorph <script> tags. See the actual DOM generated in this image

DOM

Is there an easy way to make these two play nicely together?

Is there a way to force the view to repaint? I could easily implement that after the sortable deactivate event is fired.

like image 880
wmarbut Avatar asked Jul 31 '12 20:07

wmarbut


1 Answers

It seems to me that using Ember.CollectionView, could solve this. So I gave a try. It seems to work: http://jsfiddle.net/8ahjd/

handlebars:

<script type="text/x-handlebars">
  {{view App.JQuerySortableView content=model}}
  <a {{action removeItem}}>Remove Second Item</a>
</script>

<script type="text/x-handlebars" data-template-name='jquery-sortable-item'>
  {{view.content.title}}
</script>

javascript:

App = Ember.Application.create();

App.ApplicationController = Ember.ArrayController.extend({
  removeItem: function() {
    this.removeAt(1);        
  }            
});

App.ApplicationRoute = Ember.Route.extend({
  model: function() {
    return [
      {id: 1, title:'Test 1'},
      {id: 2, title:'Test 2'},
      {id: 3, title:'Test 3'}
    ];
  }
});

App.JQuerySortableItemView = Ember.View.extend({
    templateName: 'jquery-sortable-item'        
});

App.JQuerySortableView = Ember.CollectionView.extend({
    tagName: 'ul',
    itemViewClass: App.JQuerySortableItemView, 

    didInsertElement: function(){
        this._super();
        this.$().sortable().disableSelection();
    }
});

like image 90
sly7_7 Avatar answered Sep 17 '22 01:09

sly7_7