How to create jQuery Element Methods with NameSpace -
suffice say, i'm wanting branch plugin writing bit, , decided want able "namespace" them. far, rewriting $.method
ones $.namespace.method
has been easy.
the problem i'm having making element methods such $('element').method()
, use namespace; example $('element').namespace.method()
. i've tried few workarounds , can create $.fn.namespace.method
, however, when call this
within method, $.fn.namespace
, not 'element'
i'd get.
example: if call $('body').namespace.test()
, inside method test
, want this
element <body></body>
any figuring out how pull off appreciated. over-thinking things usual.
currently trying possible work-arounds $('body').namespace().method()
, far, not working ... :p
if don't need compatible ie8, may use object.defineproperty.
working example :
object.defineproperty($.fn, 'namespace', { get: function(){ var t = this; return { lowercasehtml: function(){ return t.html(function(_,h){ return h.tolowercase() }); } } } }); $('#a').namespace.lowercasehtml(); // changes html of #a lowercase (yes, it's stupid, know)
but i'm not convinced it's idea namespace this. have defined
$.fn.namespace_lowercasehtml = function() ...
that's application specific extensions jquery.
Comments
Post a Comment