Pregunta

Tengo un contenedor de mensajes manejado con troncos relativamente simples. Puedo agregar mensajes y se muestran bien, pero agregar un retraso para cerrarlos automáticamente no funciona como undefined se pasa a la devolución de llamada.

¿Alguien podría decirme cómo especificar correctamente el argumento a llamar? La línea principal es _.delay(function(){ messages.remove(this.model) }, 3000);

var Message = Backbone.Model.extend({
    defaults: {
        message: 'No message',
        type: 'error'
    },

    /*validate: function( attrs ) {
        if ( !attrs.type in ['error', 'warning', 'success', 'info'] ) {
            return 'Wrong message type given';
        }
    }*/
});
var Messages = Backbone.Collection.extend({
    model: Message
}) 
var messages = new Messages;

var MessageView = Backbone.View.extend({
    template: Handlebars.compile( $('#t-message').html() ),

    initialize: function() {
        messages.bind('delete', this.remove, this);
    },

    render: function( event ) {
        $(this.el).html( this.template( this.model.toJSON() ) );
        // TODO: this never fires properly
        _.delay(function(){ messages.remove(this.model) }, 3000);
        return this;
    }

});

var MessageContainerView = Backbone.View.extend({
    id: 'messages',
    initialize: function() {
        messages.bind('add', this.addMessage, this);
    },
    render: function( event ) {
        return this;
    },
    addMessage: function( message ) {
        var view = new MessageView({model: message});
        $('#' + this.id).append(view.render().el);
    }
});

var messagecontainerview = new MessageContainerView;
messages.add(new Message({message: 'Close this in 3 secs.', type: 'success'}))

por supuesto this.model se define dentro del método de renderizado, pero no está definido en el remove llamar.

No hay solución correcta

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top