pombreda / base2

Automatically exported from code.google.com/p/base2
0 stars 0 forks source link

Please make the JSB rules engine usable with classes that are not behavior #115

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

Hi,

I'm now using the JSB rules engine to initialize my UI components: the 2nd
parameter of the jsb.Rule constructor is normally casted to a behavior,
however checking your code I could see that it doesn't happen if the object
you pass has a "isModification" property.

Thus, I can:
- Create a class with a "attach" static method
- Create JSB rules with new jsb.Rule( pattern, myClass )
- Have the jsb engine call myClass.attach( element )
- extend( element, this.prototype ) in my attach() method

That's a very powerful trick as it allows me to use the rules engine in the
spirit of the "tag name to custom class" bindings that are in base2.dom
even if all my tags are div ;)

However the casting I mentioned in the first paragraph makes me believe
that this is not an intended use for the Rule class, and I would like such
a use to become an official feature, so I dont have to worry about relying
on it.

Thanks,

esc0-

Original issue reported on code.google.com by mesco...@gmail.com on 14 Jun 2009 at 6:29

GoogleCodeExporter commented 9 years ago
That's an interesting idea. I will look into it.

Original comment by dean.edw...@gmail.com on 21 Jun 2009 at 3:59

GoogleCodeExporter commented 9 years ago
I've thought about this a little. I don't really want to change the code to 
allow non-
behaviors in the rule engine.

How about building a map like this:

var classMap = {
  "div.class1": Class1,
  "div.class2": Class2,
  "div.class3": Class3,
  "div.class4": Class4
};

forEach (classMap, function(Class, selector) {
  new jsb.Rule(selector, {
    onattach: function(element) {
      extend(element, Class.prototype);
    }
  });
});

BTW, did you know that you can use base2 constructors to cast objects?

extend(element, MyClass.prototype);

Is equivalent to:

MyClass(element);

That means that the loop above can be simplified to:

forEach (classMap, function(Class, selector) {
  new jsb.Rule(selector, {onattach: Class});
});

Original comment by dean.edw...@gmail.com on 19 Nov 2009 at 1:21