ronag
ronag

Reputation: 51253

Namespace Pattern Not Working?

Why does the following code complain that Model is undefined?

// models/person.js

Model = Model || {}; // ReferenceError: Model is not defined.

_.extend(Model, {
  Person: function(name) {
    this.name = name;
  }
});

var adam = new Model.Person("Adam");

Upvotes: 0

Views: 80

Answers (3)

ronag
ronag

Reputation: 51253

Based on Akhlesh answer.

I create a file defining share:

// lib/_share.js

if (Meteor.isClient) {
  share = window;
}

if (Meteor.isServer) {
  share = global;
}

And then this works:

// models/person.js

Model = share.Model || {};

Upvotes: 1

Akhlesh
Akhlesh

Reputation: 2399

Actually you should check for Model as window's property or use var.

  Model = window.Model || {}; 

OR

  var Model = Model || {};

Upvotes: 0

Paleo
Paleo

Reputation: 23692

This issue occurs in strict mode.

Try this:

var Model;
Model = Model || {};

the documentation on strict mode: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode#Converting_mistakes_into_errors

Upvotes: 0

Related Questions