<a onclick="return confirm('Are you sure?');"
href="/delete">Delete everything</a>
<script>
confirmDelete = function() {
return confirm('Are you sure?');
}
</script>
<a onclick="confirmDelete" href="/delete">Delete everything</a>
<a id="delete-link" href="/delete">Delete everything</a>
<script>
// Order matters
document.getElementById('delete-link').onclick = function() {
return confirm('Are you sure?');
}
</script>
<script>
$(function() {
$('.delete-link').on('click', function(event) {
if (!confirm('Are you sure?')) {
event.preventDefault();
alert("Saved by the bell!");
}
});
});
</script>
<a class="delete-link" href="/delete_all">Delete everything</a>
<a class="delete-link" href="/delete_some">Delete something</a>
var counter = (function() {
var counter = 0;
var inc = function() {
counter += 1;
};
var value = function() {
return counter;
}
return {
inc: inc,
value: value,
}
})();
counter.inc();
console.log(counter.value()); // 1
$('.link').on('click', function(event) {
event.preventDefault();
$.get('/data.html', function(html) {
$('.target').html(html);
};
});
var loadCallback = function(html) {
$('.target').html(html);
};
var clickCallback = function(event) {
event.preventDefault();
$.get('/data.html', loadCallback);
};
$('link').on('click', clickCallback);
var clickCallback = function(event) {
event.preventDefault();
var promise = $.get('/data.html');
promise.done(doneCallback);
}
var doneCallback = function(html) {
$('.target').html(html);
}
$('.link').on('click', clickCallback);
service.initialize = function() {
async_code_block {
trigger('init');
}
};
service.initialize();
// ... unknown amount of time passes ...
service.on('init', callback); // maybe called
service.initialize = function() {
this.promise = new Promise();
async_code_block {
this.promise.resolve();
}
}
service.initialize();
// ... unknown amount of time passes ...
service.promise.done(callback); // always called
var loaded = 0, data1, data2;
var doneCallback = function() {
if (loaded === 2) {
console.log(data1, data2);
}
}
$.get('/first_data.json', function(data) {
data1 = data;
loaded += 1;
doneCallback();
})
$.get('/second_data.json', function(data) {
data2 = data;
loaded += 1;
doneCallback();
})
var doneCallback = function(data1, data2) {
console.log(data1, data2);
})
var promise1 = $.get('/first_data.json');
var promise2 = $.get('/second_data.json');
$.when(promise1, promise2).then(doneCallback);
// n handlers, n callbacks
$('a').on('click', function() {
// ...
});
var callback = function(event) {
// ...
}
// n handlers, 1 callback
$('a').on('click', callback);
// 1 handler, 1 callback
$('body').on('click', 'a', callback);
<p class="counter">0</p>
<a class="countable" href="#">Link 1</a>
<a class="countable" href="#">Link 2</a>
$(function() {
var count = 0,
$counter = $('.counter');
$('.countable').on('click', function() {
count += 1;
$counter.html(count);
});
});
$(function() {
$('#countable').on ... // Mal
$('.countable').on ... // Regular
$('.js-countable').on ... // Algo mejor
$('[data-role~=countable]').on ... // Perito!
});
<p data-role="counter">0</p>
<a data-role="countable" href="#">Link 1</a>
<a data-role="countable" href="#">Link 2</a>
$(function() {
var count = 0,
$counter = $('[data-role~=counter]');
$('[data-role=countable]').on('click', function() {
count += 1;
$counter.html(count);
});
});
var Counter = function($counter, $links) {
this.count = 0;
this.$counter = $counter;
this.$links = $links;
var that = this;
$links.on('click', function() {
that.count += 1;
that.$counter(that.count);
};
};
$(function() {
new Counter($('[data-role~=counter]'),
$('[data-role~=countable]'));
});
var CounterDom = function($node) {
this.$node = $node;
};
CounterDom.prototype.setCount = function(count) {
this.$node.html(count);
}
var Counter = function(counterDom, $links) {
this.count = 0;
this.counterDom = counterDom;
this.$links = $links;
var that = this;
$links.on('click', function() {
that.count += 1;
that.counterDom.setCount(this.count);
};
};
$(function() {
var counterDom = new CounterDom($('[data-role~=counter]'));
new Counter(counterDom, $('[data-role~=countable]'));
});
var CountableDom = function($node) {
this.$node = $node;
};
CountableDom.prototype.onInteraction = function(callback) {
this.$node.on('click', function(event) { callback(); }
}
var Counter = function(counterDom, countableDom) {
this.count = 0;
this.counterDom = counterDom;
this.countableDom = countableDom;
var that = this;
countableDom.onInteraction(function() {
that.count += 1;
that.counterDom.setCount(this.count);
});
};
$(function() {
var counterDom = new CounterDom($('[data-role~=counter]'));
var countableDom = new CountableDom($('[data-role~=countable]'));
new Counter(counterDom, countableDom);
});
describe('Counter', function() {
var countableDomMock = {
onInteraction : function(callback) {
this.cb = callback;
}
}
var counterDomMock = {
setCount: function(value) {
this.value = value;
}
}
counter = new Counter(counterDomMock, countableDomMock);
it('counts clicks', function() {
countableDomMock.cb(); // mock interaction
expect(counterDomMock.value).toEqual(1);
});
});
<p data-role="counter">0</p>
<label>
<input type="checkbox" data-role="countable"/> Check 1
</label>
<label>
<input type="checkbox" data-role="countable"/> Check 2
</label>
var CountableDom = function($node) {
this.$node = $node;
};
CountableDom.prototype.onInteraction = function(callback) {
this.$node.on('change', function(event) { callback(); });
};
var CountableDom = function($node) {
this.$node = $node;
this.$node.on('click', function() {
$node.trigger('interaction');
});
};
CountableDom.prototype.on = function() {
this.$node.on.apply(Array.prototype.slice.call(arguments));
}
var Counter = function(counterDom, countableDom) {
// ...
countableDom.on('interaction', function() {
// ...
});
};
var Count = function(initialValue) {
this.count = initialValue;
};
Count.prototype.inc = function() {
this.count += 1;
}
Count.prototype.value = function() {
return this.count;
}
var Counter = function(count, counterDom, countableDom) {
this.count = count;
// ...
countableDom.onInteraction(function() {
that.count.increment();
that.counterDom.setCount(that.count.value());
});
};
$(function() {
var count = new Count(0);
var counterDom = new CounterDom($('[data-role~=counter]'));
var countableDom = new CountableDom($('[data-role~=countable]'));
new Counter(count, counterDom, countableDom);
});
window.eventBus = $({});
Count.prototype.inc = function() {
this.count += 1;
eventBus.trigger('count.inc', [ this.value() ]);
};
eventBus.on('count.inc', function(event, count) {
console.log(count);
};
$.fn.confirmable = function() {
this.each(function() {
var $el = $(this);
$el.on('click', function(event) {
var message = $el.data('confirm-message');
if (!confirm(message)) { /* .. */ }
});
});
};
$(function() {
$('[data-role~=confirmable]').confirmable()
});
<a data-role="confirmable"
data-confirm-message="Are you sure?"
href="/delete">Delete everything</a>
<p data-role="counter" data-initial-count="10">0</p>
CounterDom.prototype.getInitialCount = function() {
this.$node.data('initial-count');
}
var Counter = function(counterDom, countableDom) {
this.count = counterDom.getInitialCount();
// ...
}
$.get('/data.json').done(function(data) {
$placeHolder.html('<div><b>' + data.value + '</b></div>');
});
<script id="my-template" type="text/x-handlebars-template">
<div><b>{{ value }}</b></div>
</script>
$.get('/data.json').done(function(data) {
var source = $('#my-template').html(),
template = Handlebars.compile(source);
$placeHolder.html(template({value: data.value}));
});