jquery.js
backbone.pageables.js
messenger.js
pull/4/head
kay.one 12 years ago
parent 53bb6254fd
commit 4d101cc6dc

@ -1,5 +1,5 @@
/* /*
backbone-pageable 1.2.4 backbone-pageable 1.3.0
http://github.com/wyuenho/backbone-pageable http://github.com/wyuenho/backbone-pageable
Copyright (c) 2013 Jimmy Yuen Ho Wong Copyright (c) 2013 Jimmy Yuen Ho Wong
@ -91,33 +91,6 @@
return params; return params;
} }
// Quickly reset a collection by temporarily detaching the comparator of the
// given collection, reset and then attach the comparator back to the
// collection and sort.
// @param {Backbone.Collection} collection
// @param {...*} resetArgs
// @return {Backbone.Collection} collection The same collection instance after
// reset.
function resetQuickly () {
var collection = arguments[0];
var resetArgs = _.toArray(arguments).slice(1);
var comparator = collection.comparator;
collection.comparator = null;
try {
collection.reset.apply(collection, resetArgs);
}
finally {
collection.comparator = comparator;
if (comparator) collection.sort();
}
return collection;
}
var PARAM_TRIM_RE = /[\s'"]/g; var PARAM_TRIM_RE = /[\s'"]/g;
var URL_TRIM_RE = /[<>\s'"]/g; var URL_TRIM_RE = /[<>\s'"]/g;
@ -323,14 +296,15 @@
} }
if (mode != "server") { if (mode != "server") {
var fullCollection = this.fullCollection;
if (comparator && options.full) { if (comparator && options.full) {
delete this.comparator; delete this.comparator;
var fullCollection = this.fullCollection;
fullCollection.comparator = comparator; fullCollection.comparator = comparator;
fullCollection.sort();
} }
if (options.full) fullCollection.sort();
// make sure the models in the current page and full collection have the // make sure the models in the current page and full collection have the
// same references // same references
if (models && !_isEmpty(models)) { if (models && !_isEmpty(models)) {
@ -393,6 +367,7 @@
_makeCollectionEventHandler: function (pageCol, fullCol) { _makeCollectionEventHandler: function (pageCol, fullCol) {
return function collectionEventHandler (event, model, collection, options) { return function collectionEventHandler (event, model, collection, options) {
var handlers = pageCol._handlers; var handlers = pageCol._handlers;
_each(_keys(handlers), function (event) { _each(_keys(handlers), function (event) {
var handler = handlers[event]; var handler = handlers[event];
@ -486,28 +461,37 @@
else delete options.onAdd; else delete options.onAdd;
} }
if (event == "reset" || event == "sort") { if (event == "reset") {
options = collection; options = collection;
collection = model; collection = model;
if (collection == pageCol && event == "reset") { // Reset that's not a result of getPage
if (collection === pageCol && options.from == null &&
options.to == null) {
var head = fullCol.models.slice(0, pageStart); var head = fullCol.models.slice(0, pageStart);
var tail = fullCol.models.slice(pageStart + pageCol.models.length); var tail = fullCol.models.slice(pageStart + pageCol.models.length);
options = _extend(options, {silent: true}); fullCol.reset(head.concat(pageCol.models).concat(tail), options);
resetQuickly(fullCol, head.concat(pageCol.models).concat(tail),
options);
} }
else if (collection === fullCol) {
if (event == "reset" || collection == fullCol) {
if (!(state.totalRecords = fullCol.models.length)) { if (!(state.totalRecords = fullCol.models.length)) {
state.totalRecords = null; state.totalRecords = null;
state.totalPages = null; state.totalPages = null;
}
if (pageCol.mode == "client") {
state.lastPage = state.currentPage = state.firstPage; state.lastPage = state.currentPage = state.firstPage;
} }
pageCol.state = pageCol._checkState(state); pageCol.state = pageCol._checkState(state);
if (collection == pageCol) fullCol.trigger(event, fullCol, options); pageCol.reset(fullCol.models.slice(pageStart, pageEnd),
else resetQuickly(pageCol, fullCol.models.slice(pageStart, pageEnd), _extend({}, options, {parse: false}));
_extend({}, options, {parse: false})); }
}
if (event == "sort") {
options = collection;
collection = model;
if (collection === fullCol) {
pageCol.reset(fullCol.models.slice(pageStart, pageEnd),
_extend({}, options, {parse: false}));
} }
} }
@ -839,13 +823,15 @@
this.state = this._checkState(_extend({}, state, {currentPage: pageNum})); this.state = this._checkState(_extend({}, state, {currentPage: pageNum}));
options.from = currentPage, options.to = pageNum;
var pageStart = (firstPage === 0 ? pageNum : pageNum - 1) * pageSize; var pageStart = (firstPage === 0 ? pageNum : pageNum - 1) * pageSize;
var pageModels = fullCollection && fullCollection.length ? var pageModels = fullCollection && fullCollection.length ?
fullCollection.models.slice(pageStart, pageStart + pageSize) : fullCollection.models.slice(pageStart, pageStart + pageSize) :
[]; [];
if ((mode == "client" || (mode == "infinite" && !_isEmpty(pageModels))) && if ((mode == "client" || (mode == "infinite" && !_isEmpty(pageModels))) &&
!options.fetch) { !options.fetch) {
return resetQuickly(this, pageModels, _omit(options, "fetch")); return this.reset(pageModels, _omit(options, "fetch"));
} }
if (mode == "infinite") options.url = this.links[pageNum]; if (mode == "infinite") options.url = this.links[pageNum];
@ -1155,7 +1141,7 @@
data[kvp[0]] = v; data[kvp[0]] = v;
} }
var fullCollection = this.fullCollection, links = this.links; var fullCol = this.fullCollection, links = this.links;
if (mode != "server") { if (mode != "server") {
@ -1171,27 +1157,27 @@
var models = col.models; var models = col.models;
var currentPage = state.currentPage; var currentPage = state.currentPage;
if (mode == "client") resetQuickly(fullCollection, models, opts); if (mode == "client") fullCol.reset(models, opts);
else if (links[currentPage]) { // refetching a page else if (links[currentPage]) { // refetching a page
var pageSize = state.pageSize; var pageSize = state.pageSize;
var pageStart = (state.firstPage === 0 ? var pageStart = (state.firstPage === 0 ?
currentPage : currentPage :
currentPage - 1) * pageSize; currentPage - 1) * pageSize;
var fullModels = fullCollection.models; var fullModels = fullCol.models;
var head = fullModels.slice(0, pageStart); var head = fullModels.slice(0, pageStart);
var tail = fullModels.slice(pageStart + pageSize); var tail = fullModels.slice(pageStart + pageSize);
fullModels = head.concat(models).concat(tail); fullModels = head.concat(models).concat(tail);
var updateFunc = fullCollection.set || fullCollection.update; var updateFunc = fullCol.set || fullCol.update;
updateFunc.call(fullCollection, fullModels, // Must silent update and trigger reset later because the event
_extend({silent: true, sort: false}, opts)); // sychronization handler is temporarily taken out during either add
if (fullCollection.comparator) fullCollection.sort(); // or remove, which Collection#set does, so the pageable collection
fullCollection.trigger("reset", fullCollection, opts); // will be out of sync if not silenced because adding will trigger
} // the sychonization event handler
else { // fetching new page updateFunc.call(fullCol, fullModels, _extend({silent: true}, opts));
fullCollection.add(models, _extend({at: fullCollection.length, fullCol.trigger("reset", fullCol, opts);
silent: true}, opts));
fullCollection.trigger("reset", fullCollection, opts);
} }
// fetching new page
else fullCol.add(models, _extend({at: fullCol.length}, opts));
if (success) success(col, resp, opts); if (success) success(col, resp, opts);
}; };

File diff suppressed because it is too large Load Diff

@ -1,4 +1,4 @@
/*! messenger 1.3.1 2013-05-14 */ /*! messenger 1.3.2 */
/* /*
* This file begins the output concatenated into messenger.js * This file begins the output concatenated into messenger.js
* *
@ -498,7 +498,7 @@ window.Messenger.Events = (function() {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
_this.trigger("action:" + name, act, e); _this.trigger("action:" + name, act, e);
return act.action(e); return act.action.call(_this, e, _this);
}; };
})(act)); })(act));
} }
@ -810,19 +810,19 @@ window.Messenger.Events = (function() {
_ref2 = this.history; _ref2 = this.history;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) { for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
rec = _ref2[_i]; rec = _ref2[_i];
rec.$slot.removeClass('first last shown'); rec.$slot.removeClass('messenger-first messenger-last messenger-shown');
if (rec.msg.shown && rec.msg.rendered) { if (rec.msg.shown && rec.msg.rendered) {
rec.$slot.addClass('shown'); rec.$slot.addClass('messenger-shown');
anyShown = true; anyShown = true;
last = rec; last = rec;
if (willBeFirst) { if (willBeFirst) {
willBeFirst = false; willBeFirst = false;
rec.$slot.addClass('first'); rec.$slot.addClass('messenger-first');
} }
} }
} }
if (last != null) { if (last != null) {
last.$slot.addClass('last'); last.$slot.addClass('messenger-last');
} }
return this.$el["" + (anyShown ? 'remove' : 'add') + "Class"]('messenger-empty'); return this.$el["" + (anyShown ? 'remove' : 'add') + "Class"]('messenger-empty');
}; };
@ -916,12 +916,12 @@ window.Messenger.Events = (function() {
} }
}; };
ActionMessenger.prototype._getMessage = function(returnVal, def) { ActionMessenger.prototype._getHandlerResponse = function(returnVal) {
if (returnVal === false) { if (returnVal === false) {
return false; return false;
} }
if (returnVal === true || !(returnVal != null) || typeof returnVal !== 'string') { if (returnVal === true || !(returnVal != null)) {
return def; return true;
} }
return returnVal; return returnVal;
}; };
@ -965,7 +965,7 @@ window.Messenger.Events = (function() {
}; };
ActionMessenger.prototype.run = function() { ActionMessenger.prototype.run = function() {
var args, attr, events, m_opts, msg, opts, promiseAttrs, _i, _len, _ref2, _ref3, var args, attr, events, getMessageText, handler, handlers, m_opts, msg, old, opts, promiseAttrs, type, _i, _len, _ref2, _ref3,
_this = this; _this = this;
m_opts = arguments[0], opts = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : []; m_opts = arguments[0], opts = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
if (opts == null) { if (opts == null) {
@ -973,54 +973,69 @@ window.Messenger.Events = (function() {
} }
m_opts = $.extend(true, {}, this.messageDefaults, this.doDefaults, m_opts != null ? m_opts : {}); m_opts = $.extend(true, {}, this.messageDefaults, this.doDefaults, m_opts != null ? m_opts : {});
events = this._parseEvents(m_opts.events); events = this._parseEvents(m_opts.events);
getMessageText = function(type, xhr) {
var message;
message = m_opts[type + 'Message'];
if (_.isFunction(message)) {
return message.call(_this, type, xhr);
}
return message;
};
msg = (_ref2 = m_opts.messageInstance) != null ? _ref2 : this.newMessage(m_opts); msg = (_ref2 = m_opts.messageInstance) != null ? _ref2 : this.newMessage(m_opts);
if (m_opts.id != null) { if (m_opts.id != null) {
msg.options.id = m_opts.id; msg.options.id = m_opts.id;
} }
if (m_opts.progressMessage != null) { if (m_opts.progressMessage != null) {
msg.update($.extend({}, m_opts, { msg.update($.extend({}, m_opts, {
message: m_opts.progressMessage, message: getMessageText('progress', null),
type: 'info' type: 'info'
})); }));
} }
handlers = {};
_.each(['error', 'success'], function(type) { _.each(['error', 'success'], function(type) {
var old, _ref3, _ref4; var _ref3;
if ((_ref3 = opts[type]) != null ? _ref3._originalHandler : void 0) { if ((_ref3 = opts[type]) != null ? _ref3._originalHandler : void 0) {
opts[type] = opts[type]._originalHandler; opts[type] = opts[type]._originalHandler;
} }
old = (_ref4 = opts[type]) != null ? _ref4 : function() {}; return handlers[type] = function() {
opts[type] = function() { var data, defaultOpts, handlerResp, msgOpts, reason, resp, responseOpts, xhr, _base, _ref10, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9;
var data, msgOpts, msgText, r, reason, resp, xhr, _ref10, _ref11, _ref5, _ref6, _ref7, _ref8, _ref9;
resp = 1 <= arguments.length ? __slice.call(arguments, 0) : []; resp = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
_ref5 = _this._normalizeResponse.apply(_this, resp), reason = _ref5[0], data = _ref5[1], xhr = _ref5[2]; _ref4 = _this._normalizeResponse.apply(_this, resp), reason = _ref4[0], data = _ref4[1], xhr = _ref4[2];
if (type === 'success' && !(msg.errorCount != null) && m_opts.showSuccessWithoutError === false) { if (type === 'success' && !(msg.errorCount != null) && m_opts.showSuccessWithoutError === false) {
m_opts['successMessage'] = null; m_opts['successMessage'] = null;
} }
if (type === 'error') { if (type === 'error') {
if ((_ref6 = m_opts.errorCount) == null) { if ((_ref5 = m_opts.errorCount) == null) {
m_opts.errorCount = 0; m_opts.errorCount = 0;
} }
m_opts.errorCount += 1; m_opts.errorCount += 1;
} }
msgText = _this._getMessage(r = old.apply(null, resp), m_opts[type + 'Message']); handlerResp = m_opts.returnsPromise ? resp[0] : typeof (_base = opts[type])._originalHandler === "function" ? _base._originalHandler.apply(_base, resp) : void 0;
responseOpts = _this._getHandlerResponse(handlerResp);
if (_.isString(responseOpts)) {
responseOpts = {
message: responseOpts
};
}
if (type === 'error' && ((xhr != null ? xhr.status : void 0) === 0 || reason === 'abort')) { if (type === 'error' && ((xhr != null ? xhr.status : void 0) === 0 || reason === 'abort')) {
msg.hide(); msg.hide();
return; return;
} }
if (type === 'error' && ((m_opts.ignoredErrorCodes != null) && (_ref7 = xhr != null ? xhr.status : void 0, __indexOf.call(m_opts.ignoredErrorCodes, _ref7) >= 0))) { if (type === 'error' && ((m_opts.ignoredErrorCodes != null) && (_ref6 = xhr != null ? xhr.status : void 0, __indexOf.call(m_opts.ignoredErrorCodes, _ref6) >= 0))) {
msg.hide(); msg.hide();
return; return;
} }
msgOpts = $.extend({}, m_opts, { defaultOpts = {
message: msgText, message: getMessageText(type, xhr),
type: type, type: type,
events: (_ref8 = events[type]) != null ? _ref8 : {}, events: (_ref7 = events[type]) != null ? _ref7 : {},
hideOnNavigate: type === 'success' hideOnNavigate: type === 'success'
}); };
if (typeof ((_ref9 = msgOpts.retry) != null ? _ref9.allow : void 0) === 'number') { msgOpts = $.extend({}, m_opts, defaultOpts, responseOpts);
if (typeof ((_ref8 = msgOpts.retry) != null ? _ref8.allow : void 0) === 'number') {
msgOpts.retry.allow--; msgOpts.retry.allow--;
} }
if (type === 'error' && (xhr != null ? xhr.status : void 0) >= 500 && ((_ref10 = msgOpts.retry) != null ? _ref10.allow : void 0)) { if (type === 'error' && (xhr != null ? xhr.status : void 0) >= 500 && ((_ref9 = msgOpts.retry) != null ? _ref9.allow : void 0)) {
if (msgOpts.retry.delay == null) { if (msgOpts.retry.delay == null) {
if (msgOpts.errorCount < 4) { if (msgOpts.errorCount < 4) {
msgOpts.retry.delay = 10; msgOpts.retry.delay = 10;
@ -1029,7 +1044,7 @@ window.Messenger.Events = (function() {
} }
} }
if (msgOpts.hideAfter) { if (msgOpts.hideAfter) {
if ((_ref11 = msgOpts._hideAfter) == null) { if ((_ref10 = msgOpts._hideAfter) == null) {
msgOpts._hideAfter = msgOpts.hideAfter; msgOpts._hideAfter = msgOpts.hideAfter;
} }
msgOpts.hideAfter = msgOpts._hideAfter + msgOpts.retry.delay; msgOpts.hideAfter = msgOpts._hideAfter + msgOpts.retry.delay;
@ -1060,16 +1075,26 @@ window.Messenger.Events = (function() {
delete m_opts._retryActions; delete m_opts._retryActions;
} }
msg.update(msgOpts); msg.update(msgOpts);
if (msgText) { if (responseOpts && msgOpts.message) {
$.globalMessenger(); $.globalMessenger();
return msg.show(); return msg.show();
} else { } else {
return msg.hide(); return msg.hide();
} }
}; };
return opts[type]._originalHandler = old;
}); });
if (!m_opts.returnsPromise) {
for (type in handlers) {
handler = handlers[type];
old = opts[type];
opts[type] = handler;
opts[type]._originalHandler = old;
}
}
msg._actionInstance = m_opts.action.apply(m_opts, [opts].concat(__slice.call(args))); msg._actionInstance = m_opts.action.apply(m_opts, [opts].concat(__slice.call(args)));
if (m_opts.returnsPromise) {
msg._actionInstance.then(handlers.success, handlers.error);
}
promiseAttrs = ['done', 'progress', 'fail', 'state', 'then']; promiseAttrs = ['done', 'progress', 'fail', 'state', 'then'];
for (_i = 0, _len = promiseAttrs.length; _i < _len; _i++) { for (_i = 0, _len = promiseAttrs.length; _i < _len; _i++) {
attr = promiseAttrs[_i]; attr = promiseAttrs[_i];
@ -1090,6 +1115,14 @@ window.Messenger.Events = (function() {
return this.run.apply(this, [m_opts].concat(__slice.call(args))); return this.run.apply(this, [m_opts].concat(__slice.call(args)));
}; };
ActionMessenger.prototype.expectPromise = function(action, m_opts) {
m_opts = _.extend({}, m_opts, {
action: action,
returnsPromise: true
});
return this.run(m_opts);
};
return ActionMessenger; return ActionMessenger;
})(_Messenger); })(_Messenger);

Loading…
Cancel
Save