From 0d2b14e2bcdd94f9383e02d81a784081c1ce240a Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Mon, 9 Jan 2012 16:59:58 +0400 Subject: [PATCH] Allow constructor to be called without "new" --- lib/schema.js | 5 ++++- test/common_test.js | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/schema.js b/lib/schema.js index a33fa1f0..5cc5afc0 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -129,7 +129,10 @@ Schema.prototype.define = function defineClass(className, properties, settings) standartize(properties, settings); // every class can receive hash of data as optional param - var newClass = function (data) { + var newClass = function ModelConstructor(data) { + if (!(this instanceof ModelConstructor)) { + return new ModelConstructor(data); + } AbstractClass.call(this, data); }; diff --git a/test/common_test.js b/test/common_test.js index 30a2a969..71d68ace 100644 --- a/test/common_test.js +++ b/test/common_test.js @@ -108,7 +108,8 @@ function testOrm(schema) { it('should initialize object properly', function (test) { var hw = 'Hello word', now = Date.now(), - post = new Post({title: hw}); + post = new Post({title: hw}), + anotherPost = Post({title: 'Resig style constructor'}); test.equal(post.title, hw); test.ok(!post.propertyChanged('title')); @@ -118,6 +119,8 @@ function testOrm(schema) { test.strictEqual(post.published, false); test.ok(post.date >= now); test.ok(post.isNewRecord()); + test.ok(anotherPost instanceof Post); + test.ok(anotherPost.title, 'Resig style constructor'); test.done(); });