[General] About the MVC pattern

Juhani Ränkimies juhani at juranki.com
Thu Dec 18 23:33:15 CET 2008


On Thu, Dec 18, 2008 at 1:59 PM, Juhani R?nkimies <juhani at juranki.com> wrote:
> .....
> Wouldn't it be easy to add something like Record.newPlainClass(spec)?
> (at least if the default values for the properties are ignored)
> It could then be used like this:
>
> Record.newPlainClass(spec).subclass('MyModelClass', ...);
>
> where MyModelClass could define functionality around the generated
> getters and setters.
>

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.

The modification is on SVN revision 2415 and it potentially breaks
something because I changed the signature of Record.newInstance.

-juhani

-----------------------------------------

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

    newNodeClass: function(spec) { // backed by a DOM node
	return this.newClass(spec, 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 = NodeFactory.create("record"); // FIXME flat JavaScript
instead by default?
	} else {
	    storeClass = optStore 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, {});
    },

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

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





More information about the lively-kernel mailing list