Skip to content

Instantly share code, notes, and snippets.

@ericf
Created February 6, 2011 18:55
Show Gist options
  • Save ericf/813606 to your computer and use it in GitHub Desktop.
Save ericf/813606 to your computer and use it in GitHub Desktop.
New Y.Markout API
// the main difference between the new and current is convenience around writing text in node.
// also a new .node() method which return the Y.Node instance (.getNode() still exits).
// appends <h2>The New <a href="http://oddnut.com/markout/">Markout API</a></h2> to #foo
Y.Markout('#foo').h2('The New ').a({ href: 'http://oddnut.com/markout/' }, 'Markout API');
// appends <div></div> to #foo references the Y.Node instance through the .node method.
Y.Markout().div().node().appendTo('#foo');
// BUT, I was also thinking I would make Markout extend further into the library.
// I thought about extending Y.Node, to add the Y.Markout element methods to the node Prototype.
// So if you .use('gallery-markout') you'd get enhanced Y.Node instances.
Y.one('#foo').h2('Extended Y.Node with ').a({ href: 'http://oddnut.com/markout/ ' }, 'Markout API');
Y.one('body').h1('Heading ').hide().addClass('foo').span('Bla').get('parentNode').show();
@ericf
Copy link
Author

ericf commented Feb 6, 2011

So, not totally sure if I should go as far as extending Y.Node.prototype and adding all the Y.Markout.prototype methods to it (like .div(), .p(), .em(), .h1(), etc.); what do you think?

@rgrove
Copy link

rgrove commented Feb 6, 2011

I think adding methods to Y.Node.prototype is a wonderful idea. The additional flexibility and functionality it would offer would be valuable. Y.Node.addMethod() is (I think) the right way to do this.

@ericf
Copy link
Author

ericf commented Feb 6, 2011

@rgrove Yeah, I think so too! Just wanted to make sure it wasn't just me thinking crazy.
I was planning on either checking Y.Node.prototype to make sure a method didn't exist before I added one so I don't overwrite existing functionality with something completely different; or just create a blacklist of methods names to not add.

I did a quick pass over both prototypes, looks like .select() is the only overlap and either of these two options would work for writing a <select></select> node:

Y.one('#foo').el('select'); // returns a Y.Node instance wrapping the newly-appended <select>, or
Y.Markout('#foo').select().node() // returns the same as above

@kara-ryli
Copy link

One way to do that is to override some of Y.Node's methods to accept a function, a la:

Y.one("#foo").setContent(function(mo) { return mo.h2('Extended Y.Node with ').a({ href: 'http://oddnut.com/markout/ ' }, 'Markout API'); });

You could do the same with Y.Node.append and Y.Node.insert. This would avoid collisions with Y.Node's prototype methods.

@ericf
Copy link
Author

ericf commented Mar 2, 2011

@RCanine that's an interesting idea, I guess those methods (setContent, append, insert, etc) should be able to take in a Markout object as well if I go that route.

@kara-ryli
Copy link

What was your intention? Does Y.one('#foo').div() do a setContent() or an append()? I think that it's unclear is a smell that we might be missing something.

@ericf
Copy link
Author

ericf commented Mar 2, 2011

My intention would be that it would always be an append() under the hood. The Markout API is basically append-only

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment