[General] About the MVC pattern

Juhani Ränkimies juhani at juranki.com
Fri Dec 19 09:23:11 CET 2008


On Fri, Dec 19, 2008 at 12:33 AM, Juhani R?nkimies <juhani at juranki.com> wrote:
>
> I experimented by modifying Base.js:
> - added Record.newPlainClass, Record.newNodeClass, Record.newClass
> - modified Record.newPlainInstance, Record.newNodeInstance and
> Record.newInstance
>
> Quick test with Record.newPlainClass and Record.newPlainInstance worked.

Oops, cannot share rawNode with record instances of the same class.

-juhani


------------------------------
Object.extend(Record, {
	
    newPlainClass: function(spec) {
            return this.newClass(spec, function() {return {};});
    },

    newNodeClass: function(spec) { // backed by a DOM node
            return this.newClass(spec, function() {return
NodeFactory.create("record");});
    },

    newClass: function(spec, optStore) {
	if (arguments.length < 1) throw new Error("call with one or more arguments");
	var storeClass;
	var argSpec = {};
	var fieldSpec = {};
	Properties.forEachOwn(spec, function (key, value) {
	    fieldSpec[key] = {};
	    argSpec[key] = value;
	});
	if (!optStore) {
	    storeClass = lively.data.DOMNodeRecord; // FXIME forward reference
	    optStore = function() {return NodeFactory.create("record");}; //
FIXME flat JavaScript instead by default?
	} else {
            var tmpStoreInst = optStore(); // This is probably VERY
BAD for DOM records
	    storeClass = tmpStoreInst instanceof Global.Node ?
lively.data.DOMNodeRecord : PlainRecord;
	}

	var Rec = storeClass.prototype.create(fieldSpec);
        Rec.addMethods({initialize: function() {
                    this.rawNode = optStore(); // DOM or plain JS Object
                    Properties.forEachOwn(argSpec, function(key, value) {
                            this["set" + key].call(this, value);
                        }, this);
                }});
        return Rec;
    },

    newPlainInstance: function(spec) {
	return this.newInstance(spec, function() {return {};});
    },

    newNodeInstance: function(spec) { // backed by a DOM node
            return this.newInstance(spec, function() {return
NodeFactory.create("record");});
    },

    newInstance: function(spec, optStore) {
	var Rec = this.newClass(spec, optStore);
	return new Rec();
    },





More information about the lively-kernel mailing list