jquery.js
backbone.pageables.js
messenger.js
pull/24/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
Copyright (c) 2013 Jimmy Yuen Ho Wong
@ -91,33 +91,6 @@
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 URL_TRIM_RE = /[<>\s'"]/g;
@ -323,14 +296,15 @@
}
if (mode != "server") {
var fullCollection = this.fullCollection;
if (comparator && options.full) {
delete this.comparator;
var fullCollection = this.fullCollection;
fullCollection.comparator = comparator;
fullCollection.sort();
}
if (options.full) fullCollection.sort();
// make sure the models in the current page and full collection have the
// same references
if (models && !_isEmpty(models)) {
@ -393,6 +367,7 @@
_makeCollectionEventHandler: function (pageCol, fullCol) {
return function collectionEventHandler (event, model, collection, options) {
var handlers = pageCol._handlers;
_each(_keys(handlers), function (event) {
var handler = handlers[event];
@ -486,28 +461,37 @@
else delete options.onAdd;
}
if (event == "reset" || event == "sort") {
if (event == "reset") {
options = collection;
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 tail = fullCol.models.slice(pageStart + pageCol.models.length);
options = _extend(options, {silent: true});
resetQuickly(fullCol, head.concat(pageCol.models).concat(tail),
options);
fullCol.reset(head.concat(pageCol.models).concat(tail), options);
}
if (event == "reset" || collection == fullCol) {
else if (collection === fullCol) {
if (!(state.totalRecords = fullCol.models.length)) {
state.totalRecords = null;
state.totalPages = null;
}
if (pageCol.mode == "client") {
state.lastPage = state.currentPage = state.firstPage;
}
pageCol.state = pageCol._checkState(state);
if (collection == pageCol) fullCol.trigger(event, fullCol, options);
else resetQuickly(pageCol, fullCol.models.slice(pageStart, pageEnd),
_extend({}, options, {parse: false}));
pageCol.reset(fullCol.models.slice(pageStart, pageEnd),
_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}));
options.from = currentPage, options.to = pageNum;
var pageStart = (firstPage === 0 ? pageNum : pageNum - 1) * pageSize;
var pageModels = fullCollection && fullCollection.length ?
fullCollection.models.slice(pageStart, pageStart + pageSize) :
[];
if ((mode == "client" || (mode == "infinite" && !_isEmpty(pageModels))) &&
!options.fetch) {
return resetQuickly(this, pageModels, _omit(options, "fetch"));
return this.reset(pageModels, _omit(options, "fetch"));
}
if (mode == "infinite") options.url = this.links[pageNum];
@ -1155,7 +1141,7 @@
data[kvp[0]] = v;
}
var fullCollection = this.fullCollection, links = this.links;
var fullCol = this.fullCollection, links = this.links;
if (mode != "server") {
@ -1171,27 +1157,27 @@
var models = col.models;
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
var pageSize = state.pageSize;
var pageStart = (state.firstPage === 0 ?
currentPage :
currentPage - 1) * pageSize;
var fullModels = fullCollection.models;
var fullModels = fullCol.models;
var head = fullModels.slice(0, pageStart);
var tail = fullModels.slice(pageStart + pageSize);
fullModels = head.concat(models).concat(tail);
var updateFunc = fullCollection.set || fullCollection.update;
updateFunc.call(fullCollection, fullModels,
_extend({silent: true, sort: false}, opts));
if (fullCollection.comparator) fullCollection.sort();
fullCollection.trigger("reset", fullCollection, opts);
}
else { // fetching new page
fullCollection.add(models, _extend({at: fullCollection.length,
silent: true}, opts));
fullCollection.trigger("reset", fullCollection, opts);
var updateFunc = fullCol.set || fullCol.update;
// Must silent update and trigger reset later because the event
// sychronization handler is temporarily taken out during either add
// or remove, which Collection#set does, so the pageable collection
// will be out of sync if not silenced because adding will trigger
// the sychonization event handler
updateFunc.call(fullCol, fullModels, _extend({silent: true}, opts));
fullCol.trigger("reset", fullCol, opts);
}
// fetching new page
else fullCol.add(models, _extend({at: fullCol.length}, 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
*
@ -498,7 +498,7 @@ window.Messenger.Events = (function() {
e.preventDefault();
e.stopPropagation();
_this.trigger("action:" + name, act, e);
return act.action(e);
return act.action.call(_this, e, _this);
};
})(act));
}
@ -810,19 +810,19 @@ window.Messenger.Events = (function() {
_ref2 = this.history;
for (_i = 0, _len = _ref2.length; _i < _len; _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) {
rec.$slot.addClass('shown');
rec.$slot.addClass('messenger-shown');
anyShown = true;
last = rec;
if (willBeFirst) {
willBeFirst = false;
rec.$slot.addClass('first');
rec.$slot.addClass('messenger-first');
}
}
}
if (last != null) {
last.$slot.addClass('last');
last.$slot.addClass('messenger-last');
}
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) {
return false;
}
if (returnVal === true || !(returnVal != null) || typeof returnVal !== 'string') {
return def;
if (returnVal === true || !(returnVal != null)) {
return true;
}
return returnVal;
};
@ -965,7 +965,7 @@ window.Messenger.Events = (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;
m_opts = arguments[0], opts = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
if (opts == null) {
@ -973,54 +973,69 @@ window.Messenger.Events = (function() {
}
m_opts = $.extend(true, {}, this.messageDefaults, this.doDefaults, m_opts != null ? m_opts : {});
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);
if (m_opts.id != null) {
msg.options.id = m_opts.id;
}
if (m_opts.progressMessage != null) {
msg.update($.extend({}, m_opts, {
message: m_opts.progressMessage,
message: getMessageText('progress', null),
type: 'info'
}));
}
handlers = {};
_.each(['error', 'success'], function(type) {
var old, _ref3, _ref4;
var _ref3;
if ((_ref3 = opts[type]) != null ? _ref3._originalHandler : void 0) {
opts[type] = opts[type]._originalHandler;
}
old = (_ref4 = opts[type]) != null ? _ref4 : function() {};
opts[type] = function() {
var data, msgOpts, msgText, r, reason, resp, xhr, _ref10, _ref11, _ref5, _ref6, _ref7, _ref8, _ref9;
return handlers[type] = function() {
var data, defaultOpts, handlerResp, msgOpts, reason, resp, responseOpts, xhr, _base, _ref10, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9;
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) {
m_opts['successMessage'] = null;
}
if (type === 'error') {
if ((_ref6 = m_opts.errorCount) == null) {
if ((_ref5 = m_opts.errorCount) == null) {
m_opts.errorCount = 0;
}
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')) {
msg.hide();
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();
return;
}
msgOpts = $.extend({}, m_opts, {
message: msgText,
defaultOpts = {
message: getMessageText(type, xhr),
type: type,
events: (_ref8 = events[type]) != null ? _ref8 : {},
events: (_ref7 = events[type]) != null ? _ref7 : {},
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--;
}
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.errorCount < 4) {
msgOpts.retry.delay = 10;
@ -1029,7 +1044,7 @@ window.Messenger.Events = (function() {
}
}
if (msgOpts.hideAfter) {
if ((_ref11 = msgOpts._hideAfter) == null) {
if ((_ref10 = msgOpts._hideAfter) == null) {
msgOpts._hideAfter = msgOpts.hideAfter;
}
msgOpts.hideAfter = msgOpts._hideAfter + msgOpts.retry.delay;
@ -1060,16 +1075,26 @@ window.Messenger.Events = (function() {
delete m_opts._retryActions;
}
msg.update(msgOpts);
if (msgText) {
if (responseOpts && msgOpts.message) {
$.globalMessenger();
return msg.show();
} else {
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)));
if (m_opts.returnsPromise) {
msg._actionInstance.then(handlers.success, handlers.error);
}
promiseAttrs = ['done', 'progress', 'fail', 'state', 'then'];
for (_i = 0, _len = promiseAttrs.length; _i < _len; _i++) {
attr = promiseAttrs[_i];
@ -1090,6 +1115,14 @@ window.Messenger.Events = (function() {
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;
})(_Messenger);

Loading…
Cancel
Save