-2

I am quite new to backbone.js i am trying to access a var from file B to file A. currently i have declared file A as below

window.dog = Backbone.Collection.extend({

    model : animal,

    initialize : function(){
        _.bindAll(this);
    },
  // this is where i am getting an error saying i have not defined dog
  var dog=  new dog();
})

File B :

var  dog = function () {
    this.eats = {};
    this.dance.REQUEST = {};
    this.walk.REQUEST.LIST = [];

};

Now I want the method eat to be called from script A how can I do this?

T J
  • 42,762
  • 13
  • 83
  • 138
Divakar R
  • 773
  • 1
  • 8
  • 36

2 Answers2

0

It is a syntax error. you can't just define a variable inside an object.

window.dog = Backbone.Collection.extend({
 model : animal,
 initialize : function(){
  _.bindAll(this);
 },
    // change to this 
    dog :  new dog()
});
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
0

As Satish already suggested, setting dog property to the extend object will solve the issue. Also make sure dog is available in FILE A from FILE B. You could try the following:

var animal = {};
var dog = function() {
  this.eats = {
    food: 'chicken'
  };
  this.dance = {
    REQUEST: {}
  };
  this.walk = {
    REQUEST: {
      LIST: []
    }
  };
};
window.dogCollection = Backbone.Collection.extend({
  model: animal,
  initialize: function() {
    _.bindAll(this);
  },
  dog: new dog()
});
console.log(new window.dogCollection().dog.eats);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.4.0/backbone-min.js"></script>
shrys
  • 5,860
  • 2
  • 21
  • 36