doekman / base2

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

Allow for RuleList to be used on other nodes than document #71

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I've committed a change, so RuleList/Rule within JSB can be used on other 
context nodes than context.

@Dean: do you accept this?

Original issue reported on code.google.com by doek...@gmail.com on 6 Jan 2008 at 2:41

GoogleCodeExporter commented 9 years ago
Maybe. What's the use case?

Original comment by dean.edw...@gmail.com on 6 Jan 2008 at 3:58

GoogleCodeExporter commented 9 years ago
For use with my templating system. Let me explain. MiniWeb is pretty cool, but 
didn't suit my needs. So I made a 
templating system that uses plain old javascript as a templating language.

I have an echo method to writing text a a "stream" (actually an Array, abused 
as string builder). After a while, the 
string-embedded javascript became awfull. So I dediced to use JSB for that, and 
added the addCss method, which is 
adding a selector/behaviour-combo to a RuleList.

With those primitives I can build nice template functions. The top-level 
function is wrapped with a bindTo function, 
which initializes stuff, afterwards is appending the string builder to the 
innerHTML of an element, and binding the 
RuleList to the element and it's children.

One problem with this system is keeping the id-attributes of elements unique. 
This can be overcome with 
conventions, and by not using them too much. 

But binding rules on global level is cumbersome, because in that case 
id-selectors have to be used (otherwise some 
events may register twice). And it also doesn't make sense, since the template 
text part is acting on an element's 
innerHTML.

Original comment by doek...@gmail.com on 6 Jan 2008 at 4:57

GoogleCodeExporter commented 9 years ago
(changed typo in Title/Summary)

Original comment by doek...@gmail.com on 6 Jan 2008 at 4:58

GoogleCodeExporter commented 9 years ago
The changes you made to allow this introduce a bug. When you override 
RuleList.create:

RuleList.create = function(selector, behaviour) {
  return new Rule(selector, behaviour, context);
};

all new rules are now bound to this context. This applies to RuleLists that 
have been
previously created.

I must admit that I don't like this idea. It introduces an unnecessary level of
complexity. I'm not totally against the idea though. Let me look into it some 
more.

ps. I rolled back the changes you commited because of the bug you introduced.

Original comment by dean.edw...@gmail.com on 6 Jan 2008 at 5:37

GoogleCodeExporter commented 9 years ago
What about storing the context in the constructor as property, and pass it to 
Rule in RuleList.refresh?

Original comment by doek...@gmail.com on 6 Jan 2008 at 7:24

GoogleCodeExporter commented 9 years ago
I think I have it (roll back if you don't like it). The weird thing is the call 
to refresh within the constructor of Rule. 
I moved it to the constructor of RuleList.

This is of course changing the behaviour of the Rule-class.

I still think the "auto-refresh" (applying the RuleList on construction-time) 
is a bit weird, but understandable. I 
also added the option to apply an instance of RuleList to another node.

Original comment by doek...@gmail.com on 6 Jan 2008 at 8:23

GoogleCodeExporter commented 9 years ago
I'm not sure about these changes yet so I'm going to roll them back.

Let me think about this issue for a while..

Original comment by dean.edw...@gmail.com on 6 Jan 2008 at 9:27

GoogleCodeExporter commented 9 years ago
I made a small change to the Rule object that makes it easier to extend JSB.

Rule can now take a Selector object in the constructor function. That means you 
can
mess with the Selector object to achieve the result you wanted:

JSB.RuleList = JSB.RuleList.extend({
  constructor: function(rules, context) {
    extend(this, "put", function(selector, rule) {
      selector = new DOM.Selector(selector);
      extend(selector, "exec", function() {
        return this.base(context);
      });
      return this.base(selector, rule);
    });
    this.base(rules);
  }
});

Please mark this issue "fixed" if you are happy with this solution.

Original comment by dean.edw...@gmail.com on 7 Jan 2008 at 12:31

GoogleCodeExporter commented 9 years ago

Original comment by doek...@gmail.com on 7 Jan 2008 at 7:49