سؤال

In my Backbone.js app, one of my views renders a table via a template.

var myView = Backbone.View.extend({
    el: 'table',
    render: function() {
        this.$el.html(template({ this.model.attributes });
    }
});

I need to bind some data to the table rows. But they are created within the template. Is it ok to add a method to my view where I bind data to each element using jQuery's .data() function or is that considered bad practice in Backbone circles?

Edit: My implicit question is if there's a more Backboneish way to do this, if the answer is that it is considered bad practice.

هل كانت مفيدة؟

المحلول

That would be considered a code smell: the goal for Backbone is to manage application state in javascript INSTEAD of shoving stuff in the DOM. If you want to render a collection take a look at (e.g.) http://www.codebeerstartups.com/2012/12/9-collection-views-in-backbone-js-learning-backbone-js/#.Uuol5K3JjmE

Or if you're open to Backbone-based frameworks, look at Marionette's composite view (which is a Marionette collection view that will allow you to display tables). There's a blog post on that here: http://lostechies.com/derickbailey/2012/04/05/composite-views-tree-structures-tables-and-more/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top