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

Default data #18

Open
wants to merge 4 commits into
base: devel
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
17 changes: 17 additions & 0 deletions layout-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
{{> yield region="footer"}}
</template>

<template name="LayoutThatSetsData">
{{#with childData}}
{{> yield}}
{{/with}}
{{#with childData}}
{{> yield region="footer"}}
{{/with}}
</template>

<template name="ChildWithData">
child {{title}}
</template>
Expand Down Expand Up @@ -58,3 +67,11 @@
<template name="TemplateWithCreatedCallback">
callback
</template>

<template name="DefaultDataForLayout">
{{#with title="ok"}}
{{#Layout template="LayoutWithOneYield"}}
inner{{title}}
{{/Layout}}
{{/with}}
</template>
32 changes: 30 additions & 2 deletions layout-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ Tinytest.add('layout - default main region using Layout template', function (tes
});
});

Tinytest.add('layout - default data context using Layout template', function (test) {
withRenderedComponent(Template.DefaultDataForLayout, function (cmp, screen) {
test.equal(screen.innerHTML.compact(), 'layoutinnerok', 'default data context should be outer data context');
});
});

Tinytest.add('layout - dynamic yield regions', function (test) {
withRenderedLayout({template: 'LayoutWithTwoYields'}, function (layout, screen) {
var renderedCount = 1;
Expand Down Expand Up @@ -224,7 +230,6 @@ Tinytest.add('layout - region templates not found in lookup', function (test) {
});



// SEE IR#276 for detailed discussion
Tinytest.add('layout - Templates render with correct data even if setData is called after setRegion', function (test) {
withRenderedLayout({template: 'LayoutWithOneYield'}, function (layout, screen) {
Expand All @@ -243,4 +248,27 @@ Tinytest.add('layout - Templates render with correct data even if setData is cal
Deps.flush();
test.equal(screen.innerHTML.compact(), 'layoutcallback');
});
});
});

// XXX: This test doesn't work.
// To be totally honest, I'm not sure how it *should* work -- should
// the yield be getting the data context of the with block? Maybe..
// perhaps yield.data() should look at parent's data (modolo __isTemplateWith)
// just like layout does.
//
// Tinytest.add('layout - set data via with', function (test) {
// withRenderedLayout({template: 'LayoutThatSetsData'}, function (layout, screen) {
// layout.setRegion('main', 'ChildWithData');
// layout.setRegion('footer', 'FooterWithData');
//
// layout.setData({
// title: 'parentTitle',
// childData: {
// title: 'childTitle'
// }
// });
//
// Deps.flush();
// test.equal(screen.innerHTML.compact(), 'childchildTitlefooterchildTitle');
// });
// });
26 changes: 24 additions & 2 deletions layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Layout = UI.Component.extend({
var tmplDep = new Deps.Dependency;

// get the initial data value
var data = Deps.nonreactive(function () { return self.get(); });
var data, dataSet = false;
var dataDep = new Deps.Dependency;
var regions = this._regions = new ReactiveDict;
var content = this.__content;
Expand Down Expand Up @@ -136,10 +136,26 @@ Layout = UI.Component.extend({
var cachedData = Deps.cache(function () {
log('return data()');
dataDep.depend();
return data;
if (dataSet) {
return data;
} else {
// find the closest parent with a data context.
// If it's the direct parent, and it has `__isTemplateWith` set,
// then it's because we have `{{#Layout foo=bar}}` and we should ignore
var parent = self.parent;
if (parent) {
if (parent.__isTemplateWith)
parent = parent.parent;
return getComponentData(parent);
} else {
// the only time we don't have a parent is when we are in tests really
return null
}
}
});

this.setData = function (value) {
dataSet = true;
log('setData', value);
if (!EJSON.equals(value, data)) {
data = value;
Expand Down Expand Up @@ -204,6 +220,12 @@ Layout = UI.Component.extend({
// reset the data function to use the layout's
// data
this.data = function () {
// XXX: should we be instead trying to sensibly get parent's
// data -- much like layout.getData() does?
// then we'd expect to inherit layout.getData() (via layout.render)
// unless we wrapped our {{> yield}} in a with.
// is this what users would expect?
// see 'layout - set data via with' test
return layout.getData();
};
},
Expand Down