Skip to content
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

Fix: treat invalid model with data as valid during view tree creation #651

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/ns.view.js
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,9 @@
};

/**
*
* Returns models for view tree.
* It consists of models and their statuses (ok / error).
* If any of the models has error status - ns-view-error-content mode will be rendered for that view.
* @returns {*}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Точно returns models? А то эта строчка говорит any (в сам код, без дифа не смотрел, может быть там все правильно)

* @private
*/
Expand All @@ -1041,7 +1043,10 @@
/** @type ns.Model */
var model = models[id];
modelsData[id] = {};
if (model.isValid()) {
// treat model as valid in case
// - it is really a valid one
// - it has invalid status but in has real data too (@see #649)
if (model.isValid() || (model.status === this.STATUS.INVALID && !!model.getData())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а если зашить это в isValid — страшно (куча мест развалится)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Страшно да ..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

окай

// successful model status
modelsData[id].status = 'ok';
// structure for convenient matching
Expand Down
68 changes: 68 additions & 0 deletions test/spec/ns.view.js
Original file line number Diff line number Diff line change
Expand Up @@ -1501,4 +1501,72 @@ describe('ns.View', function() {

});

describe('#_getModelsForTree', function() {
beforeEach(function() {
ns.Model.define('good-data');
ns.Model.get('good-data').setData({ foo: 'bar' });

ns.Model.define('with-error');
ns.Model.get('with-error').setError({ error: 'what was that?' });

ns.Model.define('just-created');

ns.Model.define('invalidated-with-data');
ns.Model.get('invalidated-with-data').setData({ forever: 'sure' });
ns.Model.get('invalidated-with-data').invalidate();

ns.Model.define('invalidated-with-no-data');
ns.Model.get('invalidated-with-no-data').setData({ forever: 'almost sure' });
ns.Model.get('invalidated-with-no-data')._reset(ns.M.STATUS.INVALID);

ns.Model.define('destroyed');
ns.Model.get('destroyed').setData({ foo: 'baz' });
ns.Model.get('destroyed').destroy();

ns.View.define('view', {
params: { id: null },
models: [
'good-data',
'with-error',
'just-created',
'invalidated-with-data',
'invalidated-with-no-data',
'destroyed'
]
});

this.view = ns.View.create('view', { id: 'app' });
});

it('complex test with different models in different states', function() {
expect(this.view._getModelsForTree()).to.be.eql({
'good-data': {
status: 'ok',
'good-data': { foo: 'bar' }
},
'with-error': {
status: 'error',
'with-error': { error: 'what was that?' }
},
'just-created': {
status: 'error',
'just-created': null
},
'invalidated-with-data': {
status: 'ok',
'invalidated-with-data': { forever: 'sure' } // @see #649
},
'invalidated-with-no-data': {
status: 'error',
'invalidated-with-no-data': null
},
'destroyed': {
status: 'error',
'destroyed': null
}
});
});

});

});