New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Выравнивание логики уничтожения моделей #174
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -451,7 +451,8 @@ ns.Model.prototype.getRequestParams = function() { | |
* @returns {ns.Model} | ||
*/ | ||
ns.Model.get = function(id, params) { | ||
var model = ns.Model.find(id, params); | ||
|
||
var model = this._find(id, params); | ||
|
||
if (!model) { | ||
var Ctor = _ctors[id]; | ||
|
@@ -468,18 +469,32 @@ ns.Model.get = function(id, params) { | |
}; | ||
|
||
/** | ||
* Returns cached model instance. | ||
* Returns valid cached model instance. | ||
* @param {String} id Model's ID. | ||
* @param {Object} [params] Model's params | ||
* @returns {ns.Model|undefined} | ||
* @returns {ns.Model|null} | ||
*/ | ||
ns.Model.find = function(id, params) { | ||
var model = this._find(id, params); | ||
if (model && model.isValid()) { | ||
return model; | ||
} | ||
return null; | ||
}; | ||
|
||
/** | ||
* Returns cached model instance. | ||
* @param {String} id Model's ID. | ||
* @param {Object} [params] Model's params | ||
* @returns {ns.Model|null} | ||
*/ | ||
ns.Model._find = function(id, params) { | ||
if (!(id in _infos)) { | ||
throw new Error('[ns.Model] "' + id + '" is not defined'); | ||
} | ||
|
||
var key = ns.Model.key(id, params); | ||
return _cache[id][key]; | ||
return _cache[id][key] || null; | ||
}; | ||
|
||
/** | ||
|
@@ -496,9 +511,6 @@ ns.Model.destroy = function(model) { | |
|
||
var cached = _cache[id][key]; | ||
if (cached) { | ||
// remove from cache | ||
delete _cache[id][key]; | ||
|
||
// notify subscribers about disappearance | ||
model.trigger('ns-model-destroyed'); | ||
|
||
|
@@ -508,8 +520,8 @@ ns.Model.destroy = function(model) { | |
}; | ||
|
||
// Проверяем, есть ли модель в кэше и валидна ли она. | ||
ns.Model.isValid = function(id, key) { | ||
var model = ns.Model.get(id, key); | ||
ns.Model.isValid = function(id, params) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вот тут как-то жёстко меняется api. А зачем? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. просто меняется имя переменной =) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. сюда всегда по факту передаются параметры There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ааааа ) |
||
var model = ns.Model.get(id, params); | ||
if (!model) { return; } // undefined означает, что кэша нет вообще, а false -- что он инвалидный. | ||
|
||
return model.isValid(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А как мне теперь получить любую закешированную модель?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Поговорили голосом.
А как мне теперь получить любую закешированную модель?
С помощью ns.Model.get. Теперь нет существенной разницы между отсутствием экземпляра модели и наличием невалидного экземпляра.
Договорились переименовать метод find в getValid, чтобы название было правдой.