-->

保存jQuery UI的可排序的顺序来Backbone.js的集合(Saving jQuery UI

2019-06-23 15:30发布

我有一个Backbone.js的集合,我希望能够使用jQuery UI的可排序进行排序。 没什么特别的,我只是,我想能够排序列表。

问题是,我不知道如何分类之后获得项目的当前秩序,传达到集合。 排序可以序列化本身,但不会给我我需要给到集合中的模型数据。

理想情况下,我想能够只得到集合中的模型的当前顺序的排列,并使用重置方法集合,但我不知道如何让当前的订单。 请分享任何想法或例子用来获取与现款车型顺序的数组。

Answer 1:

我已经使用jQuery UI可排序,当一个项目被丢弃到触发项目视图的事件做到了这一点。 然后我可以触发包括所述模型作为集合视图绑定到数据项目视图的另一事件。 然后集合视图可以负责更新排序顺序。

工作实例

http://jsfiddle.net/7X4PX/260/

jQuery用户界面可排序

$(document).ready(function() {
    $('#collection-view').sortable({
        // consider using update instead of stop
        stop: function(event, ui) {
            ui.item.trigger('drop', ui.item.index());
        }
    });
});

所述止挡事件绑定到触发功能drop ,用于与项目的索引(由jQuery的用户界面提供)作为数据项的DOM节点上。

项目视图

Application.View.Item = Backbone.View.extend({
    tagName: 'li',
    className: 'item-view',
    events: {
        'drop' : 'drop'
    },
    drop: function(event, index) {
        this.$el.trigger('update-sort', [this.model, index]);
    },        
    render: function() {
        $(this.el).html(this.model.get('name') + ' (' + this.model.get('id') + ')');
        return this;
    }
});

放置事件被绑定到drop触发的功能update-sort与数据项目视图的DOM节点上事件[this.model, index] 。 这意味着我们传递当前模型和它的指数(从jQuery UI的排序)给谁就给谁,势必对update-sort事件。

项目(集)视图

Application.View.Items = Backbone.View.extend({
    events: {
        'update-sort': 'updateSort'
    },
    render: function() {
        this.$el.children().remove();
        this.collection.each(this.appendModelView, this);
        return this;
    },    
    appendModelView: function(model) {
        var el = new Application.View.Item({model: model}).render().el;
        this.$el.append(el);
    },
    updateSort: function(event, model, position) {            
        this.collection.remove(model);

        this.collection.each(function (model, index) {
            var ordinal = index;
            if (index >= position) {
                ordinal += 1;
            }
            model.set('ordinal', ordinal);
        });            

        model.set('ordinal', position);
        this.collection.add(model, {at: position});

        // to update ordinals on server:
        var ids = this.collection.pluck('id');
        $('#post-data').html('post ids to server: ' + ids.join(', '));

        this.render();
    }
}); 

Items视图绑定到update-sort事件和功能使用由事件(模型和索引)传递的数据。 该模型是从集合中删除,该ordinal属性更新每个剩余项目,通过ID项目的顺序被发送到服务器来存储状态。

采集

Application.Collection.Items = Backbone.Collection.extend({
    model: Application.Model.Item,
    comparator: function(model) {
        return model.get('ordinal');
    },
});

收集具有比较定义函数的命令所收集ordinal 。 这使得同步的项目,如收集的“默认顺序”的渲染顺序是现在的价值ordinal属性。

需要注意的工作有一些重复:该模型并不需要被删除并重新添加到收藏集合是否具有作为的jsfiddle做了比较功能。 还认为可能不需要重新渲染本身。

:相比于其他的答案,我的感觉是,这是更正确的通知,它需要直接而不是更新的集合的项目的模型实例。 这两种方法都是有效的。 这里的其他答案直接进入集合,而不是取模型的第一种方法。 无论选择哪种更有意义给你。



Answer 2:

http://jsfiddle.net/aJjW6/2/

HTML:

`<div class="test-class">
     <h1>Backbone and jQuery sortable - test</h1>
     <div id="items-collection-warper"></div>
</div>`

JavaScript的:

$(document).ready(function(){

var collection = [
    {name: "Item ", order: 0},
    {name: "Item 1", order: 1},
    {name: "Item 2", order: 2},
    {name: "Item 3", order: 3},
    {name: "Item 4", order: 4}
];
var app = {};

app.Item = Backbone.Model.extend({});
app.Items = Backbone.Collection.extend({
    model: app.Item,
    comparator: 'order',

});

app.ItemView = Backbone.View.extend({
    tagName: 'li',
    template: _.template('<span><%= name %> - <b><%= order %></b></span>'),
    initialize: function(){

    },
    render: function(){
        var oneItem = this.$el.html(this.template(this.model.attributes));
        return this;
    }
});

app.AppView = Backbone.View.extend({
    el: "#items-collection-warper",
    tagName: 'ul',
    viewItems: [],
    events:{
        'listupdate': 'listUpdate'
    },
    initialize: function(){
        var that = this;

        this.$el.sortable({
             placeholder: "sortable-placeholder",
            update: function(ev, ui){
               that.listUpdate();
            }
        });            
    },
    render: function(){
        var that= this;
        this.collection.each(function(item){
            that.viewItems.push(that.addOneItem(item));
            return this;
        });

    },
    addOneItem: function(item){
        var itemView = new app.ItemView({model: item});
        this.$el.append(itemView.render().el);

        return itemView;
    },

    listUpdate: function(){


        _.each(this.viewItems, function(item){
            item.model.set('order', item.$el.index());
        });
        this.collection.sort({silent: true})
         _.invoke(this.viewItems, 'remove');
        this.render();
    }
});

var Items = new app.Items(collection)
var appView = new app.AppView({collection: Items});
appView.render();
});

CSS:

.test-class{
    font-family: Arial;
}
.test-class li{
    list-style:none;
    height:20px;

}
.test-class h1{
    font-size: 12px;
}
.ui-sortable-helper{
    opacity:0.4;
}
.sortable-placeholder{
    background: #ddd;
    border:1px dotted #ccc;
}


Answer 3:

只要使用Backbone.CollectionView !

var collectionView = new Backbone.CollectionView( {
  sortable : true,
  collection : new Backbone.Collection
} );

瞧!



文章来源: Saving jQuery UI Sortable's order to Backbone.js Collection