使用Backbone.js创建后为HTML元素设置动画

时间:2012-06-21 11:44:22

标签: jquery backbone.js

我的代码如下。在这种情况下,如何使用类似动画的JQuery调整元素大小?详细信息在代码中写为注释。

模板:

<script id="content_template" type="text/template">
        <div
                class="content"
                style="position: absolute;
                    top:<%= top %>px;
                    left:<%= left %>px;
                    width:<%= width %>px;
                    height:<%= height %>px; "
        ></div>
</script>

型号:

var FrameObject = Backbone.Model.extend({
    defaults: {
        top: 0,
        left: 0,
        width: 1,
        height: 1
    }
});

收藏:

var FrameObjects = Backbone.Collection.extend({
    model: FrameObject
});

对象视图:

var Object_view = Backbone.View.extend({
    template: _.template($("#content_template").html()),
    render:function(){
        return this.template(this.model.toJSON());
    }
});

对象容器视图:

var App_view = Backbone.View.extend({
    el: $(".container"),
    events: {
        "click .container": "create_object"
    },
    create_object: function(event){
        var frame_object = new FrameObject({
            top: event.pageX,
            left: event.pageY,
            width: 100,
            height: 100
        });
        frame_objects.add(frame_object);

        var object_view = new Object_view({model: frame_object});
        this.$(".container").append(object_view.render());

        /* here I want to change the 'top' & 'left' attributes of the object model
           and want my object_view to animate to that position. Same as it would do for -

           someElement.animate({top: newTop, left: newLeft});
        */
    }
});

1 个答案:

答案 0 :(得分:8)

我写了一个简单的例子:

var FrameObject = Backbone.Model.extend({
    defaults: {
        top: 0,
        left: 0,
        width: 1,
        height: 1
    }
});

var FrameObjects = Backbone.Collection.extend({
    model: FrameObject
});

var ObjectView = Backbone.View.extend({
    className: 'frame',

    initialize: function() {
        this.model.on('change', this.animate, this);
    },

    render: function() {
        var params = _.extend(this.model.toJSON(),  { position: 'absolute' });
        this.$el.css(params);
        return this;
    },

    animate: function() {
        this.$el.animate(this.model.toJSON());
    }
});

var AppView = Backbone.View.extend({
    el: $("#container"),

    events: {
        "click": "createObject"
    },

    createObject: function(event) {
        var frameObject = new FrameObject({
            top: event.pageY,
            left: event.pageX,
            width: 100,
            height: 100
        });

        var frameObjects = new FrameObjects();

        frameObjects.add(frameObject);

        var objectView = new ObjectView({
            model: frameObject
        });
        this.$el.append(objectView.render().el);

        setTimeout(function() {
            frameObject.set({
                // 500 — width of #container
                top: Math.random() * 500,
                left: Math.random() * 500
            })
        }, 1000);
    }
});

var appView = new AppView();

http://jsfiddle.net/theotheo/4Y8gy/

注意我对您的代码进行了一些更改。首先,我更改了AppView的events哈希值,或者更确切地说,我省略了selectorbecause

  

省略选择器会导致事件绑定到视图的根元素(this.el)。

看起来它正是你想要的。其次,我删除了ObjectView的模板。当然,你可以使用它,但在这种情况下,我认为没有必要。第三,我重命名了变量for the sake of consistency。 随意问。