Search code examples
ruby-on-railsruby-on-rails-3.1backbone.jsdust.js

How to call dust template from backbone view


Hi. I am trying to call dust template from a backbone view in rails project.

This is the way of calling JST template:

Spa.Views.PostsIndex = Backbone.View.extend({

template: JST['posts/index'],

I need to replace erb with dust. I am using dust_assets gem to render dust templates.

I have a file index.jst.dust template under app/assets/templates/post.

When I call the template from application.js file like below, I am able to render the dust template, but I am not able to render it from backbone view.

$(function() {


 JST["templates/index"]({ name : "World" }, function(err, out) {
 $('#dust').html(out);
  });
});

Please suggest me how to call the dust template or point me to some link which explains the same.


Solution

  • backbone.js's View class has a no-op render method - this means you have to override this method to tell the View class how to render it's view.

    you will have to do this even though you've defined the template variable in the class.

    an example would be:

    render: function() {
      this.template({name: "World"}, function(err, out){
        $(this.el).html(out); 
      });
      return this;
    }