Commits

Mark Vasilkov committed 2aad44d

Return value from parent constructor

Comments (0)

Files changed (4)

 	paths: {
 		backbone: 'lib/backbone/backbone',
 		bootstrap: 'lib/bootstrap/bootstrap',
-		'class': 'lib/misato/class',
 		jquery: 'lib/jquery/jquery',
 		lodash: 'lib/lodash/lodash',
 		mustache: 'lib/mustache/mustache',
 require.config({
 	baseUrl: '',
 	paths: {
-		'class': 'lib/misato/class',
-		class_test: 'lib/misato/test/class_test',
-		class_test_data: 'lib/misato/test/class_test_data'
+		backbone: 'lib/backbone/backbone',
+		backbone_super: 'lib/misato/backbone_super',
+		jquery: 'lib/jquery/jquery',
+		lodash: 'lib/lodash/lodash',
+		super_test: 'lib/misato/test/super_test',
+		super_test_data: 'lib/misato/test/super_test_data'
+	},
+	shim: {
+		backbone: {
+			deps: ['lodash', 'jquery'],
+			exports: 'Backbone'
+		}
 	}
 })
 
-require(['class_test'], function() {
+require(['super_test'], function() {
 	QUnit.start()
 })

lib/misato/test/super_test.js

-define(['class', 'class_test_data'], function(Class, t) {
-	module('Class')
+define(['backbone_super', 'super_test_data'], function(Backbone, t) {
+	module('backbone_super.js')
 
 	test('Subclasses (level 1)', function() {
 		strictEqual(typeof t.Lvl1, 'function', 'Subclass is of type "function"')
 
 		strictEqual(typeof t.lvl1, 'object', 'New subclass is of type "object"')
 		ok(t.lvl1 instanceof t.Lvl1, 'New subclass is instance of subclass')
-		ok(t.lvl1 instanceof Class, 'New subclass is instance of Class')
+		ok(t.lvl1 instanceof Backbone.Model, 'New subclass is instance of Backbone.Model')
 		strictEqual(t.lvl1.constructor, t.Lvl1, "New subclass' constructor is ok")
 
 		strictEqual(t.lvl1._value, 9, 'Subclass#initialize is called')
 	test('Subclasses (level 2)', function() {
 		ok(t.lvl2 instanceof t.Lvl2, 'New subclass is instance of subclass')
 		ok(t.lvl2 instanceof t.Lvl1, 'New subclass is instance of parent')
-		ok(t.lvl2 instanceof Class, 'New subclass is instance of Class')
+		ok(t.lvl2 instanceof Backbone.Model, 'New subclass is instance of Backbone.Model')
 
 		strictEqual(t.lvl2._value, 9, 'Parent#initialize is called')
 		ok(!('_super' in t.lvl2), 'New subclass has no "_super" field')
 		t.lvl3.dec().dec().dec()
 		strictEqual(t.lvl3._value, 69, 'Methods pass sanity test (_super) (2)')
 	})
+
+	test('Return value from parent constructor', function() {
+		strictEqual(t.Model.saved(), t.m1, "Don't discard return value (1)")
+		strictEqual(t.m1, t.m2, "Don't discard return value (2)")
+
+		ok(t.m1 instanceof t.Model, 'New subclass is instance of subclass (1)')
+		ok(t.m2 instanceof t.Model, 'New subclass is instance of subclass (2)')
+	})
 })

lib/misato/test/super_test_data.js

-define(['class'], function(Class) {
-	var Lvl1 = Class.extend({
+define(['backbone_super'], function(Backbone) {
+	var Lvl1 = Backbone.Model.extend({
 		_value: 0,
 
 		initialize: function() {
 		}
 	})
 
+	/* Return value from parent constructor */
+
+	var saved, Base = Backbone.Model.extend({
+		constructor: function(args, force) {
+			if (force) return Backbone.Model.call(this, args)
+			else return this.constructor.create(args)
+		}
+	}, {
+		create: function(args) {
+			return saved = saved || new this(args, true)
+		}
+	})
+
+	var Model = Base.extend({}, {
+		saved: function() {return saved}
+	})
+
 	return {
 		Lvl1: Lvl1,
 		Lvl2: Lvl2,
 
 		lvl1: new Lvl1,
 		lvl2: new Lvl2,
-		lvl3: new Lvl3
+		lvl3: new Lvl3,
+
+		Model: Model,
+
+		m1: new Model,
+		m2: new Model
 	}
 })