What’s the difference between: $(this.el).html and this.$el.html

$(this.el) wraps an element with jQuery (or Zepto). So, if your view HTML was this: <div id=”myViewElement”></div> …and this.el referenced that div, then $(this.el) would be the equivalent of retrieving it directly via jQuery: $(‘#myViewElement’). this.$el is a cached reference to the jQuery (or Zepto) object, so a copy of what you would get from … Read more

Backbone.js and pushState

This is the pattern Tim Branyen uses in his boilerplate: initializeRouter: function () { Backbone.history.start({ pushState: true }); $(document).on(‘click’, ‘a:not([data-bypass])’, function (evt) { var href = $(this).attr(‘href’); var protocol = this.protocol + ‘//’; if (href.slice(protocol.length) !== protocol) { evt.preventDefault(); app.router.navigate(href, true); } }); } Using that, rather than individually doing preventDefault on links, you let … Read more

Backbone model.destroy() invoking error callback function even when it works fine?

@David Tuite comment: “Ok I figured it out. It seems that Backbone expects the JSON response to be a JSON serialization of the record that was destroyed. However, Rails controller generators only return head :ok by default. I changed my JSON response to be render json: @listing_save where @listing_save is the record I just destroyed … Read more

Backbone.js + Rest. Collection is not populated after fetch()

I think you are on the right way. But because Backbone.Collection.fetch() is async, you should check value of entityList.models not right after the method call, but in success callback of fetch. That is, this code will say that models list is empty: entityList.fetch(); console.log(entityList.models); // => 0 (collection being fetched) while this code will print … Read more

Best strategy to use HAML template with Backbone.js

I know you already mentioned it but I would suggest using haml-js with Jammit. Simply include haml.js in your javascripts and in your assets.yml add template_function: Haml as well as including your template files in to a package. e.g. javascript_templates: – app/views/**/*.jst.haml Then in your views you can include this package (= include_javascripts :javascript_templates) and … Read more

Backbone: event lost in re-render

When you do this: this.$el.html( this.subView.render().el ); You’re effectively saying this: this.$el.empty(); this.$el.append( this.subView.render().el ); and empty kills the events on everything inside this.$el: To avoid memory leaks, jQuery removes other constructs such as data and event handlers from the child elements before removing the elements themselves. So you lose the delegate call that binds … Read more

navigate route with querystring

You need to add another route with that expecting parameter : routes: { ‘posts?foo=:foo’ : ‘showPosts’, ‘posts’: ‘showPosts’ }, showPosts: function (foo) { if(typeof foo != ‘undefined’){ // foo parameters was passed } test = true; } update You could define the general route to return all the query string and then parse it in … Read more

backbone.js use different urls for model save and fetch

If you’re reading the source you probably already have a working solution. You essentially have two options (probably more)- Pass URL in save()/fetch() save() takes two parameters attr and options attr – is a hash of model attributes and is used to update the model before save. eg. myModel.save(attrs) is equivalent to myModel.set(attrs) myModel.save() The … Read more