Skip to content

Commit 3bbc847

Browse files
authored
Merge pull request #229 from regularjs/issue189
fix #189
2 parents 2bfd8c7 + a5141dc commit 3bbc847

File tree

5 files changed

+85
-24
lines changed

5 files changed

+85
-24
lines changed

dist/regular.js

+27-10
Original file line numberDiff line numberDiff line change
@@ -2145,17 +2145,34 @@ return /******/ (function(modules) { // webpackBootstrap
21452145
return filter;
21462146
},
21472147
// simple accessor get
2148-
_sg_:function(path, defaults, ext){
2148+
// ext > parent > computed > defaults
2149+
_sg_:function(path, parent, defaults, ext){
21492150
if( path === undefined ) return undefined;
2151+
21502152
if(ext && typeof ext === 'object'){
21512153
if(ext[path] !== undefined) return ext[path];
21522154
}
2153-
var computed = this.computed,
2154-
computedProperty = computed[path];
2155-
if(computedProperty){
2156-
if(computedProperty.type==='expression' && !computedProperty.get) this._touchExpr(computedProperty);
2157-
if(computedProperty.get) return computedProperty.get(this);
2158-
else _.log("the computed '" + path + "' don't define the get function, get data."+path + " altnately", "warn")
2155+
2156+
// reject to get from computed, return undefined directly
2157+
// like { empty.prop }, empty equals undefined
2158+
// prop shouldn't get from computed
2159+
if(parent === null) {
2160+
return undefined
2161+
}
2162+
2163+
if(parent && typeof parent[path] !== 'undefined') {
2164+
return parent[path]
2165+
}
2166+
2167+
// without parent, get from computed
2168+
if (parent !== null) {
2169+
var computed = this.computed,
2170+
computedProperty = computed[path];
2171+
if(computedProperty){
2172+
if(computedProperty.type==='expression' && !computedProperty.get) this._touchExpr(computedProperty);
2173+
if(computedProperty.get) return computedProperty.get(this);
2174+
else _.log("the computed '" + path + "' don't define the get function, get data."+path + " altnately", "warn")
2175+
}
21592176
}
21602177

21612178
if( defaults === undefined ){
@@ -3191,7 +3208,7 @@ return /******/ (function(modules) { // webpackBootstrap
31913208
pathes = [];
31923209
pathes.push( path );
31933210
last = path;
3194-
base = ctxName + "._sg_('" + path + "', " + varName + ", " + extName + ")";
3211+
base = ctxName + "._sg_('" + path + "', undefined, " + varName + ", " + extName + ")";
31953212
onlySimpleAccessor = true;
31963213
}else{ //Primative Type
31973214
if(path.get === 'this'){
@@ -3217,7 +3234,7 @@ return /******/ (function(modules) { // webpackBootstrap
32173234
var tmpName = this.match('IDENT').value;
32183235
prevBase = base;
32193236
if( this.la() !== "(" ){
3220-
base = ctxName + "._sg_('" + tmpName + "', " + base + ")";
3237+
base = ctxName + "._sg_('" + tmpName + "', " + base + " || null)";
32213238
}else{
32223239
base += "." + tmpName ;
32233240
}
@@ -3229,7 +3246,7 @@ return /******/ (function(modules) { // webpackBootstrap
32293246
if( this.la() !== "(" ){
32303247
// means function call, we need throw undefined error when call function
32313248
// and confirm that the function call wont lose its context
3232-
base = ctxName + "._sg_(" + path.get + ", " + base + ")";
3249+
base = ctxName + "._sg_(" + path.get + ", " + base + " || null)";
32333250
}else{
32343251
base += "[" + path.get + "]";
32353252
}

dist/regular.min.js

+4-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/parser/Parser.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ op.member = function(base, last, pathes, prevBase){
554554
pathes = [];
555555
pathes.push( path );
556556
last = path;
557-
base = ctxName + "._sg_('" + path + "', " + varName + ", " + extName + ")";
557+
base = ctxName + "._sg_('" + path + "', undefined, " + varName + ", " + extName + ")";
558558
onlySimpleAccessor = true;
559559
}else{ //Primative Type
560560
if(path.get === 'this'){
@@ -580,7 +580,7 @@ op.member = function(base, last, pathes, prevBase){
580580
var tmpName = this.match('IDENT').value;
581581
prevBase = base;
582582
if( this.la() !== "(" ){
583-
base = ctxName + "._sg_('" + tmpName + "', " + base + ")";
583+
base = ctxName + "._sg_('" + tmpName + "', " + base + " || null)";
584584
}else{
585585
base += "." + tmpName ;
586586
}
@@ -592,7 +592,7 @@ op.member = function(base, last, pathes, prevBase){
592592
if( this.la() !== "(" ){
593593
// means function call, we need throw undefined error when call function
594594
// and confirm that the function call wont lose its context
595-
base = ctxName + "._sg_(" + path.get + ", " + base + ")";
595+
base = ctxName + "._sg_(" + path.get + ", " + base + " || null)";
596596
}else{
597597
base += "[" + path.get + "]";
598598
}

lib/render/client.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -553,17 +553,34 @@ Regular.implement({
553553
return filter;
554554
},
555555
// simple accessor get
556-
_sg_:function(path, defaults, ext){
556+
// ext > parent > computed > defaults
557+
_sg_:function(path, parent, defaults, ext){
557558
if( path === undefined ) return undefined;
559+
558560
if(ext && typeof ext === 'object'){
559561
if(ext[path] !== undefined) return ext[path];
560562
}
561-
var computed = this.computed,
562-
computedProperty = computed[path];
563-
if(computedProperty){
564-
if(computedProperty.type==='expression' && !computedProperty.get) this._touchExpr(computedProperty);
565-
if(computedProperty.get) return computedProperty.get(this);
566-
else _.log("the computed '" + path + "' don't define the get function, get data."+path + " altnately", "warn")
563+
564+
// reject to get from computed, return undefined directly
565+
// like { empty.prop }, empty equals undefined
566+
// prop shouldn't get from computed
567+
if(parent === null) {
568+
return undefined
569+
}
570+
571+
if(parent && typeof parent[path] !== 'undefined') {
572+
return parent[path]
573+
}
574+
575+
// without parent, get from computed
576+
if (parent !== null) {
577+
var computed = this.computed,
578+
computedProperty = computed[path];
579+
if(computedProperty){
580+
if(computedProperty.type==='expression' && !computedProperty.get) this._touchExpr(computedProperty);
581+
if(computedProperty.get) return computedProperty.get(this);
582+
else _.log("the computed '" + path + "' don't define the get function, get data."+path + " altnately", "warn")
583+
}
567584
}
568585

569586
if( defaults === undefined ){

test/spec/browser-bugfix.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,32 @@ it('bugfix #50', function(){
14051405

14061406

14071407
})
1408-
1408+
1409+
describe("M 0.6.1", function(){
1410+
it('bug #189: computed override', function(){
1411+
var App = Regular.extend({
1412+
template: '<div ref=data>{ content.title }</div><div ref=computed>{ title }</div><div ref=empty>{ empty.title }</div>',
1413+
computed: {
1414+
title() {
1415+
return 'from_computed'
1416+
}
1417+
},
1418+
});
1419+
var app = new App({
1420+
data: {
1421+
title: 'from_data_title',
1422+
content: {
1423+
title: 'from_data'
1424+
}
1425+
}
1426+
});
1427+
var $data = app.$refs.data;
1428+
var $computed = app.$refs.computed;
1429+
var $empty = app.$refs.empty;
1430+
expect($data.innerHTML).to.equal('from_data');
1431+
expect($computed.innerHTML).to.equal('from_computed');
1432+
expect($empty.innerHTML).to.equal('');
1433+
})
1434+
})
14091435

14101436
})

0 commit comments

Comments
 (0)