How to fetch Backbone App file variables in Views using Commonjs

73 views Asked by At

I have a Backbone App which contains data like this:

App = new Backbone.Marionette.Application({
url: 'http://localhost/index.php',
quotes:
    [
        {text: "simple text" name: "John Doe"},
    ]
});

$(document).ready(function() {
    App.start();
});

And if I want to fetch quotes in my View file, I can simply use it like:

var myView = Backbone.Marionette.ItemView.extend({
render: function() {
    console.log(App.quotes);
}

Now I want to add commonJS support in my Backbone application, So I have modified App file code something like this:

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

App = new Backbone.Marionette.Application({
url: 'http://localhost/index.php',
quotes:
    [
        {text: "simple text" name: "John Doe"},
    ]
});

$(document).ready(function() {
    App.start();
});

module.exports = App;

And after adding the commonJS file support in views file, it becomes something like this:

var Backbone = require('backbone');
var App = require('app');

var myView = Backbone.Marionette.ItemView.extend({
render: function() {
    console.log(App.quotes);
}

module.exports = myView;

But the problem is, I am not able to get value of

App.quotes

The value of App variable is empty object. Please let me know what am I doing wrong.

1

There are 1 answers

0
Amarjeet Kaur On

Problem sorted out. I tried adding the require statement after initialize function, and it worked.