Archive for BackboneJS

Router::refresh() utility method in BackboneJS

BackboneJS’s Router does not provide an easy way to run an action twice.

In a previous article I wrote a simple workaround to this problem, now I packed it into a Backbone extension:

_.extend(Backbone.Router.prototype,{

    refresh: function() {

        var _tmp = Backbone.history.fragment;

        this.navigate( _tmp + (new Date).getTime() );

        this.navigate( _tmp, { trigger:true } );

    }

});

Enjoy it!

Posted in BackboneJS | Leave a comment

BackboneKIT Boilerplate jsFiddle

I’m working hard on BackboneKIT to supply to developer community a tool to create BackboneJS apps faster.

To help you try my BackboneKIT project I created a jsFiddle packed with:

  • jQuery
  • Underscore
  • BackboneJS
  • BackboneKIT
  • TwitterBootstrap (CSS)

Try BackboneKIT Boilerplate on jsFiddle!

Continue reading »

Posted in BackboneJS | 2 Comments

BackboneJS Declarative Events

Today I merged into my BackboneKIT project a piece of code from backbone.declarative GitHub page to implement declarative events into Views. Continue reading »

Posted in BackboneJS | Leave a comment

BackboneJS Inheritance by BackboneKIT

I often use to create a layered components app where each object may extend an existing one: An Image extends a Panel, a Panel extends a Component.

In BackboneKIT each layer can extend parent’s layer with the Backbone.View::extend() method (as normal) but some thing goes slightly different from the BackboneJS way. Continue reading »

Posted in BackboneJS | Leave a comment

BackboneJS Plugin System with BackboneKIT

When think about big Js projects I often need to pack some behaviors together then add to multiple classes. I often need this with views.

BackboneJS itself offer no solutions to accomplish this stuff but I’m a curious person so I started to surf and read then I found this git. That inspired me.

I think it’s time to add a “plugin” concept to the BackboneJS library. I did it in my BackboneKit project but I hope someone else will code it better! Continue reading »

Posted in BackboneJS | Leave a comment

Backbone: how to refresh a Router action

There is a little bug in BackboneJS Router’s logic: If you try to trigger the same route twice (or more) then linked action is triggered only once.

In this simple tutorial I tell you why this behavior happens and I propose a simple workaround to let things works. I also set up a jsFiddle example to demonstrate how this bug influence your app and the simple workaround to kill it.

Continue reading »

Posted in BackboneJS, Tips & Tricks | 4 Comments

BackboneJS: how to use HTML Node as underscoreJS Template

BackboneJS uses UnderscoreJS and UnderscoreJS embeds a micro template system who can be used in many situations.

In my LearningBackbone Project I play with templates in Example 007. In 4th column of that example I use the original column content as template.

This is the code that make it works:

SCRIPT vs DOM as templates:

Official tutorials teachs to use a SCRIPT block so store a templates string:

// page.html
<script type="text/template" id="my_template">
Hi, <%= person.name %>!
</script>

// foo_view.js
var FooView = Backbone.View.extend({
    template: _.template( $('#my_template').html() )
});

But i think may be often simple to write our template directly into DOM nodes so that template can works as text placeholder too:

// page.html
<div id="say_hello">Hi, <%= person.name %>!</div>

This DIV block contain an UnderscoreJS’s template string. When run our script we need to fetch template from the DIV, parse it and, later, populate the DIV with rendered content.

If you try to load the page without running Javascript this template works as text placeholder so you can test your design!

// foo_view.js
var FooView = Backbone.View.extend({
    initialize: function() {
        this.template = $('#say_hello').html();
        this.template = $('<textarea/>').html( this.template ).val();
        this.template = _.template( this.template );
        $('#say_hello').html('');
    }
});

This code may appear a little verbose because I need to explain it row by row… you can pack it into 1 line!

The naked code:

#04> this.template = $('#say_hello').html();

fetch the html content of target DOM node.

#05> this.template = $('<textarea/>').html( this.template ).val();

There is a problem! jQuery::html() method convert some characters with their entities! In particular our fetched template have &lt; and &lt; in place of < and >!

The solution is to pass this test into a TEXTAREA field who’s not applied to the document DOM then fetch it’s value with the jQuery::val() method who returns what we need!

#06> this.template = _.template( this.template );

Just compile the template text with UnderscoreJS::template() method. Now we have a ready to use template function who can receives data and returns compiled texts!

#007> $('#say_hello').html('');

Just empty the template container. This instruction may not be important because ofter view’s render() method replace the container contents anyway!

The problem is jQuery!

jQuery::html() converts special characters into entities, this become a problem with Underscore’s template syntax <% … %>.

// Template source
... <%= name %> ...

// this.$el.html() returns:
... &lt;= name &gt; ...

This way the content fetched from the DOM is not a correct Underscore template anymore!

The solution: TEXTAREA!

Fortunately the solution lyes into jQuery too!

You can create a virtual TEXTAREA field, this is not a performance sink because this element will not be added to the page’s DOM… so no re-rendering in triggered to the browser.

TEXTAREA can be populated with some HTML contents with or without entities tag brakets.

Then you can get TEXTAREA source with the val() method who returns plain text code ready to be compiled by the Underscore::template() engine!

Posted in BackboneJS, Tips & Tricks | Leave a comment

Swedish Greys - a WordPress theme from Nordic Themepark.