Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

make $() wrap JS objects, only trigger events on DOM elements; fixes #…

  • Loading branch information...
commit b25065ce22740421c3f1f890ff0d4d81ef463770 1 parent b3547a8
@madrobby madrobby authored
Showing with 49 additions and 27 deletions.
  1. +5 −1 src/event.js
  2. +3 −0  src/zepto.js
  3. +38 −26 test/ajax.html
  4. +3 −0  test/zepto.html
View
6 src/event.js
@@ -164,7 +164,11 @@
if (typeof event == 'string') event = $.Event(event)
fix(event)
event.data = data
- return this.each(function(){ this.dispatchEvent(event) })
+ return this.each(function(){
+ // items in the collection might not be DOM elements
+ // (todo: possibly support events on plain old objects)
+ if('dispatchEvent' in this) this.dispatchEvent(event)
+ })
}
// triggers event handlers on current element just as if an event occurred,
View
3  src/zepto.js
@@ -26,6 +26,7 @@ var Zepto = (function() {
function isFunction(value) { return ({}).toString.call(value) == "[object Function]" }
function isObject(value) { return value instanceof Object }
+ function isJSObject(value) { return ({}).toString.call(value) == "[object Object]" }
function isArray(value) { return value instanceof Array }
function likeArray(obj) { return typeof obj.length == 'number' }
@@ -86,6 +87,8 @@ var Zepto = (function() {
else {
var dom
if (isArray(selector)) dom = compact(selector)
+ else if (isJSObject(selector))
+ dom = [$.extend({}, selector)], selector = null
else if (elementTypes.indexOf(selector.nodeType) >= 0 || selector === window)
dom = [selector], selector = null
else if (fragmentRE.test(selector))
View
64 test/ajax.html
@@ -134,7 +134,7 @@
t.pause()
var el = testEl.load('fixtures/ajax_load_simple.html', function(){
var that = this
- deferredResume(t, function() {
+ deferredResume(t, function(){
this.assertIdentical(testEl, that)
this.assertEqual('simple ajax load', testEl.html().trim())
})
@@ -145,34 +145,34 @@
testAjaxLoadWithSelector: function(t) {
var testEl = $('#ajax_load')
t.pause()
- testEl.load('fixtures/ajax_load_selector.html #ajax_load_test_div', function() {
+ testEl.load('fixtures/ajax_load_selector.html #ajax_load_test_div', function(){
deferredResume(t, function() {
this.assertEqual('ajax load with selector', testEl.html().trim())
})
})
},
- testAjaxLoadWithJavaScript: function (t) {
- var testEl = $('#ajax_load')
- t.pause()
- window.testValue = 0
- testEl.load('fixtures/ajax_load_selector_javascript.html', function() {
- deferredResume(t, function () {
- this.assertEqual(window.testValue, 1)
- })
- })
- },
-
- testAjaxLoadWithSelectorAndJavaScript: function (t) {
- var testEl = $('#ajax_load')
- t.pause()
- window.testValue = 0
- testEl.load('fixtures/ajax_load_selector_javascript.html #ajax_load_test_div', function() {
- deferredResume(t, function () {
- this.assertEqual(window.testValue, 0)
- })
- })
- },
+ testAjaxLoadWithJavaScript: function (t) {
+ var testEl = $('#ajax_load')
+ t.pause()
+ window.testValue = 0
+ testEl.load('fixtures/ajax_load_selector_javascript.html', function(){
+ deferredResume(t, function(){
+ this.assertEqual(window.testValue, 1)
+ })
+ })
+ },
+
+ testAjaxLoadWithSelectorAndJavaScript: function (t) {
+ var testEl = $('#ajax_load')
+ t.pause()
+ window.testValue = 0
+ testEl.load('fixtures/ajax_load_selector_javascript.html #ajax_load_test_div', function() {
+ deferredResume(t, function(){
+ this.assertEqual(window.testValue, 0)
+ })
+ })
+ },
testAjaxWithContext: function(t) {
t.pause()
@@ -180,13 +180,25 @@
$.ajax({
url: 'fixtures/ajax_load_simple.html',
context: body,
- complete: function () {
+ complete: function(){
var that = this
- deferredResume(t, function() {
+ deferredResume(t, function(){
this.assertIdentical(body, that)
})
}
})
+ },
+
+ testAjaxWithObjectContext: function(t) {
+ t.pause()
+ var context = {}
+ $.ajax({
+ url: 'fixtures/ajax_load_simple.html',
+ context: context,
+ complete: function(){
+ deferredResume(t)
+ }
+ })
}
})
@@ -403,7 +415,7 @@
t.assertEqual(MockXHR.last, xhr)
},
- testBeforeSendCallback: function(t) {
+ testBeforeSendAbortCallback: function(t) {
var xhr, beforeFired = false, settings = {}
$.ajax({
data: "1=2",
View
3  test/zepto.html
@@ -332,6 +332,9 @@
var zepto = $(['a', 'b', 'c'])
t.assertLength(3, zepto)
t.assertEqualCollection(['a', 'b', 'c'], zepto)
+
+ t.assert($({}))
+ t.assertTrue($({ a:true })[0].a)
},
testGetWithoutIndex: function(t){
Please sign in to comment.
Something went wrong with that request. Please try again.