Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Remove 'new' keyword when creating a can.Observe

$
0
0
Let me start off by saying that I've only been working with canJS for about 2 weeks. I'm new, but i came across something that caused me a few hours of frustration and I thought could be improved to help future users.

The issue was that can.Observe objects must be instantiated with the new keyword.
  1. var observer = new can.Observe( datasource ); 
This is fine, except that all other canJS object builders do not require use of the 'new' keyword, as they return constructor functions.

  1. var Todo = can.Model(...);
  2. var t = new Todo(...)
There is nothing wrong with all this, except that it's a bit inconsistent. Almost all 'can.' namespace functions are used without the 'new' keyword, except for can.Observe. It's easy to forget to use 'new' and spend hours figuring out why your data object isn't working. 

I'm not sure if a self invoking constructor could be implemented, but i personally think it would be helpful. 

  1. if (!(this instanceof can.Observe)) {
  2.         return new can.Observe(datasource);
  3. }



Viewing all articles
Browse latest Browse all 3491

Trending Articles