acacia
acacia

Reputation: 1387

Backbone: pass variables to appended template

How can i pass variables in an appended template on a template. I am adding flash messages to show errors;

P.S am new to backbone

Here is the malfunctioning code;

error: function(user, response){
    //create flash message with errors and render new page
    var failureErrors = $.parseJSON(response.responseText).errors;
    var errorView = new Skymama.Views.ErrorMessages();                      
    $("#error_messages").append(errorView.render().el({errors: failureErrors}));
}

Here is the template;

<div class="alert alert-danger">
    <button type="button" class="close" data-dismiss="alert">
        <i class="icon-remove"></i>
    </button>
    <div id="error_list">
        <ul>
            <%  errors.each(function(error){ %>

            <% }); %> 
        </ul>
    </div>
    <br>
</div>

Upvotes: 0

Views: 91

Answers (1)

Yurui Zhang
Yurui Zhang

Reputation: 2232

In your code, I see you are trying to pass params to el - which is not a function.

You can simply add a parameter in your ErrorMessages view's render method to take errors:

//in your Skymama.Views.ErrorMessages...

render: function (templateData) {
    this.$el.html(this.template(templateData));
}

and in your error handler:

$("#error_messages").append(errorView.render({errors: failureErrors}));

There are various ways to improve this, e.g. you can make failureErrors a collection and pass it to the errorView, and use collection events to handle the rendering.

Upvotes: 1

Related Questions