Nominalista
Nominalista

Reputation: 4838

Backbone.js + Require.js and ViewsFactory

Let's assume that we have testing code like this:

var App = (function () {

    var api = {
        Router: null,
        init: function () {
            this.content = $("#content");
            Backbone.history.start();
            return this;
        }
    };

    var ViewsFactory = {


        view1: function () {
            var model1 = new model1();
            return new api.Views.View1({
                model: model1
            });
        },

        view2: function () {
            var model2 = new model2();
            return new api.Views.View2({
                model: model2
            });
        },

        view3: function () {
            var model3 = new model3();
            return new api.Views.View3({
                model: model3
            });
        },


    };

    var Router = Backbone.Router.extend({
        routes: {
            "": "view1",
            "2": "view2",
            "3": "view3",
        },

        view1: function () {
            var view1 = ViewsFactory.view1();
            $(".content").html(view1.render().el);
        },  

        view2: function () {
            var view2 = ViewsFactory.view2();
            $(".content").html(view2.render().el);
        },

        view3: function () {
            var view3 = ViewsFactory.view3();
            $(".content").html(view3.render().el);
        },

    });

    api.Router = new Router();

    return api;
})();

And I want to use Require.js. Please don't focus on names, but on the idea.

  1. If I understand it correctly, I have to include in require method every view (View1, View2, View3) and every model (Model1, Model2, Model3). But what is the purpose of using Require.js in such case instead of traditional <script> tags?
  2. Using ViewsFactory is a good practice in backbone projects?

Upvotes: 1

Views: 63

Answers (2)

Nominalista
Nominalista

Reputation: 4838

The best option in router is using variables like this:

 var $ = require('jquery'),
     Backbone    = require('backbone');

Upvotes: 0

Fran&#231;ois Richard
Fran&#231;ois Richard

Reputation: 7055

WHy not a view factory. In your case I'm not sure it's really useful though.

requirejs will help to build reusable modules. http://requirejs.org/docs/why.html

Upvotes: 1

Related Questions