Skip to content

Commit 11599d6

Browse files
Kaloyan PavlovIankodj
authored andcommitted
feat(kendo.View): Add support for useWithBlock
1 parent 57047fb commit 11599d6

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

docs/api/javascript/view.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ If set to `true`, the view template will be treated as kendo template and evalua
2525
view.render($("#app"));
2626
</script>
2727

28+
### useWithBlock `Boolean`*(default: true)*
29+
30+
If set to `false` and evalTemplate is set to `true`, the kendo template will be evaluated without using a `with` block.
31+
32+
#### Example
33+
34+
<div id="app"></div>
35+
<script id="foo-template" type="text/x-kendo-template">
36+
<span>#: data.foo #</span> <!-- "data." is required as there is no with block -->
37+
</script>
38+
<script>
39+
var foo = { foo: "foo" }
40+
var view = new kendo.View('foo-template', { model: foo, evalTemplate: true });
41+
view.render($("#app"));
42+
</script>
43+
2844
### model `ObservableObject`*(default: null)*
2945

3046
The MVVM model to bind the element to.

src/kendo.view.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ var __meta__ = { // jshint ignore:line
7575
that.model = options.model;
7676
that._wrap = options.wrap !== false;
7777
this._evalTemplate = options.evalTemplate || false;
78+
this._useWithBlock = options.useWithBlock;
7879
that._fragments = {};
7980

8081
that.bind([ INIT, SHOW, HIDE, TRANSITION_START, TRANSITION_END ], options);
@@ -213,11 +214,11 @@ var __meta__ = { // jshint ignore:line
213214
content = that.content;
214215
}
215216
}
216-
217+
217218
if (typeof content === "string") {
218219
content = content.replace(/^\s+|\s+$/g, '');
219220
if (that._evalTemplate) {
220-
content = kendo.template(content)(that.model || {});
221+
content = kendo.template(content, { useWithBlock: that._useWithBlock })(that.model || {});
221222
}
222223

223224
element = $(wrapper).append(content);
@@ -232,7 +233,7 @@ var __meta__ = { // jshint ignore:line
232233
} else {
233234
element = content;
234235
if (that._evalTemplate) {
235-
var result = $(kendo.template($("<div />").append(element.clone(true)).html())(that.model || {}));
236+
var result = $(kendo.template($("<div />").append(element.clone(true)).html(), { useWithBlock: that._useWithBlock })(that.model || {}));
236237

237238
// template uses DOM
238239
if ($.contains(document, element[0])) {

tests/view/view.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,29 @@
101101
assert.equal(Mocha.fixture.find("#foo").html(), "foo");
102102
});
103103

104+
it("evaluates template without with block when useWithBlock is false", function() {
105+
var view = new kendo.View("<div>#: data.foo.bar #</div>", { model: { foo: { bar: "bar" } }, evalTemplate: true, useWithBlock: false });
106+
assert.equal(view.render().html(), "<div>bar</div>");
107+
});
108+
109+
it("fails to evaluate template without with block when useWithBlock is false and the template refers to the properties of data directly", function() {
110+
var view = new kendo.View("<div>#: foo.bar #</div>", { model: { foo: { bar: "bar" } }, evalTemplate: true, useWithBlock: false });
111+
assert.throws(() => view.render());
112+
});
113+
114+
it("evaluates template with an element in the dom without with block when useWithBlock is false", function() {
115+
Mocha.fixture.append("<div id=fooSuccess>#: data.foo.bar #</div>")
116+
var view = new kendo.View("#fooSuccess", { model: { foo: { bar: "bar" } }, evalTemplate: true, useWithBlock: false });
117+
view.render();
118+
assert.equal(Mocha.fixture.find("#fooSuccess").html(), "bar");
119+
});
120+
121+
it("fails to evaluate template with an element in the dom without with block when useWithBlock is false and the template refers to the properties of data directly", function() {
122+
Mocha.fixture.append("<div id=fooFailure>#: foo.bar #</div>")
123+
var view = new kendo.View("#fooFailure", { model: { foo: { bar: "bar" } }, evalTemplate: true, useWithBlock: false });
124+
assert.throws(() => view.render());
125+
});
126+
104127
it("can skip wrapping", function() {
105128
var view = new kendo.View("<span id='foo'>Foo</span>", { wrap: false });
106129

typescript/kendo.all.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ declare namespace kendo {
345345
wrap?: boolean;
346346
model?: Object;
347347
evalTemplate?: boolean;
348+
useWithBlock?: boolean;
348349
init?: (e: ViewEvent) => void;
349350
show?: (e: ViewEvent) => void;
350351
hide?: (e: ViewEvent) => void;

0 commit comments

Comments
 (0)