Commits

Anonymous committed 2ef02c8

Add PyQuery.fn for hooking custom function (like that of jQuery.fn) as per issue #16

Comments (0)

Files changed (1)

pyquery/pyquery.py

             results.remove()
         return self
 
+    class Fn(object):
+        """Hook for defining custom funcion (like the jQuery.fn)
+        
+        >>> PyQuery.fn.listOuterHtml = lambda: this.map(lambda i, el: PyQuery(this).outerHtml())
+        >>> S = PyQuery('<ol>   <li>Coffee</li>   <li>Tea</li>   <li>Milk</li>   </ol>')
+        >>> S('li').listOuterHtml()
+        ['<li>Coffee</li>', '<li>Tea</li>', '<li>Milk</li>']
+        
+        """
+        def __setattr__(self, name, func):
+            def fn(self, *args):
+                func.func_globals['this'] = self
+                return func(*args)
+            fn.__name__ = name
+            setattr(PyQuery, name, fn)
+    fn = Fn()
+    
     #####################################################
     # Additional methods that are not in the jQuery API #
     #####################################################
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.