Appending el in Backbone from an instance

2019-01-29 10:10发布

I'm attempting to append the html within el from instances

var Article1 = Backbone.View.extend({
    initialize: function(options) {
        console.log('type 1', options)
        this.flyIn(options);
    },
    flyIn: function(options) {
        this.$el.find('.superDog').css({
            display: 'block'
        });
        this.$el.find('.superDog').animate({
            top: options.top,
            left: options.left
        }, 3000);
    },
    render: function() {

    },
    el: '<div><p class="superDog"></p></div>'
});

var Article1A = new Article1({
    color: 'black',
    top: '300',
    left: '300',
    html: 'hello world'

});

var Article1B = new Article1({
    color: 'blue',
    top: '800',
    left: '800',
    html: 'Hello Everyone'
});

var Article1C = new Article1({
    color: 'blue',
    top: '600',
    left: '1000',
    html: 'Hello No One'
});

I've tried putting append.el (or el.append, wasn't sure which way it went), options.html, etc.

Is it possible to do what I'm trying to do or do I have to use something else?

1条回答
Deceive 欺骗
2楼-- · 2019-01-29 10:37

Take time to read the Backbone documentation:

  • the el view property is a string (a selector) or DOM element,
  • the this.$el refer to a jQuery object of this.el which is the DOM element.
  • template is whatever you want, a function or a string mostly.

Optimize your use of jQuery by caching objects instead of selecting them over and over.

// chain when possible
this.$('.superDog')
    .append(options.html)
    .css({ display: 'block' });

// cache the object
var $superDog = this.$('.superDog');

// and use it later
$superDog.append(options.html);
// ...more code and then...
$superDog.css({ display: 'block' });

Rendering a dynamic list

Make a generic view which will serve for every article.

var ArticleView = Backbone.View.extend({
    tagName: 'p',
    className: 'superDog',
    render: function() {
        // apply the template
        this.$el.html(this.model.get('html'))
            .show()
            .animate({
                top: this.model.get('top'),
                left: this.model.get('left')
            }, 3000);

        return this; // always return this in render.
    },
});

Then use this view inside a generic article list view.

var ArticleList = Backbone.View.extend({
    render: function() {
        this.$el.empty();
        this.collection.each(this.renderArticle, this);
        return this;
    },
    renderArticle: function(model) {
        var view = new ArticleView({
            model: model
        });
        this.$el.append(view.render().el);
    },
});

And use Backbone models and a collection to convey the data.

var list = new ArticleList({
    collection: new Backbone.Collection([{
            id: '1A',
            color: 'black',
            top: '300',
            left: '300',
            html: 'hello world'

        },
        {
            id: '1B',
            color: 'blue',
            top: '800',
            left: '800',
            html: 'Hello Everyone'
        },
        {
            id: '1C',
            color: 'blue',
            top: '600',
            left: '1000',
            html: 'Hello No One'
        }
    ])
});

list.render();
查看更多
登录 后发表回答