Skip to content

Commit

Permalink
Merge pull request #229 from regularjs/issue189
Browse files Browse the repository at this point in the history
fix #189
  • Loading branch information
leeluolee authored Nov 22, 2018
2 parents 2bfd8c7 + a5141dc commit 3bbc847
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 24 deletions.
37 changes: 27 additions & 10 deletions dist/regular.js
Original file line number Diff line number Diff line change
Expand Up @@ -2145,17 +2145,34 @@ return /******/ (function(modules) { // webpackBootstrap
return filter;
},
// simple accessor get
_sg_:function(path, defaults, ext){
// ext > parent > computed > defaults
_sg_:function(path, parent, defaults, ext){
if( path === undefined ) return undefined;

if(ext && typeof ext === 'object'){
if(ext[path] !== undefined) return ext[path];
}
var computed = this.computed,
computedProperty = computed[path];
if(computedProperty){
if(computedProperty.type==='expression' && !computedProperty.get) this._touchExpr(computedProperty);
if(computedProperty.get) return computedProperty.get(this);
else _.log("the computed '" + path + "' don't define the get function, get data."+path + " altnately", "warn")

// reject to get from computed, return undefined directly
// like { empty.prop }, empty equals undefined
// prop shouldn't get from computed
if(parent === null) {
return undefined
}

if(parent && typeof parent[path] !== 'undefined') {
return parent[path]
}

// without parent, get from computed
if (parent !== null) {
var computed = this.computed,
computedProperty = computed[path];
if(computedProperty){
if(computedProperty.type==='expression' && !computedProperty.get) this._touchExpr(computedProperty);
if(computedProperty.get) return computedProperty.get(this);
else _.log("the computed '" + path + "' don't define the get function, get data."+path + " altnately", "warn")
}
}

if( defaults === undefined ){
Expand Down Expand Up @@ -3191,7 +3208,7 @@ return /******/ (function(modules) { // webpackBootstrap
pathes = [];
pathes.push( path );
last = path;
base = ctxName + "._sg_('" + path + "', " + varName + ", " + extName + ")";
base = ctxName + "._sg_('" + path + "', undefined, " + varName + ", " + extName + ")";
onlySimpleAccessor = true;
}else{ //Primative Type
if(path.get === 'this'){
Expand All @@ -3217,7 +3234,7 @@ return /******/ (function(modules) { // webpackBootstrap
var tmpName = this.match('IDENT').value;
prevBase = base;
if( this.la() !== "(" ){
base = ctxName + "._sg_('" + tmpName + "', " + base + ")";
base = ctxName + "._sg_('" + tmpName + "', " + base + " || null)";
}else{
base += "." + tmpName ;
}
Expand All @@ -3229,7 +3246,7 @@ return /******/ (function(modules) { // webpackBootstrap
if( this.la() !== "(" ){
// means function call, we need throw undefined error when call function
// and confirm that the function call wont lose its context
base = ctxName + "._sg_(" + path.get + ", " + base + ")";
base = ctxName + "._sg_(" + path.get + ", " + base + " || null)";
}else{
base += "[" + path.get + "]";
}
Expand Down
7 changes: 4 additions & 3 deletions dist/regular.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lib/parser/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ op.member = function(base, last, pathes, prevBase){
pathes = [];
pathes.push( path );
last = path;
base = ctxName + "._sg_('" + path + "', " + varName + ", " + extName + ")";
base = ctxName + "._sg_('" + path + "', undefined, " + varName + ", " + extName + ")";
onlySimpleAccessor = true;
}else{ //Primative Type
if(path.get === 'this'){
Expand All @@ -580,7 +580,7 @@ op.member = function(base, last, pathes, prevBase){
var tmpName = this.match('IDENT').value;
prevBase = base;
if( this.la() !== "(" ){
base = ctxName + "._sg_('" + tmpName + "', " + base + ")";
base = ctxName + "._sg_('" + tmpName + "', " + base + " || null)";
}else{
base += "." + tmpName ;
}
Expand All @@ -592,7 +592,7 @@ op.member = function(base, last, pathes, prevBase){
if( this.la() !== "(" ){
// means function call, we need throw undefined error when call function
// and confirm that the function call wont lose its context
base = ctxName + "._sg_(" + path.get + ", " + base + ")";
base = ctxName + "._sg_(" + path.get + ", " + base + " || null)";
}else{
base += "[" + path.get + "]";
}
Expand Down
31 changes: 24 additions & 7 deletions lib/render/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,17 +553,34 @@ Regular.implement({
return filter;
},
// simple accessor get
_sg_:function(path, defaults, ext){
// ext > parent > computed > defaults
_sg_:function(path, parent, defaults, ext){
if( path === undefined ) return undefined;

if(ext && typeof ext === 'object'){
if(ext[path] !== undefined) return ext[path];
}
var computed = this.computed,
computedProperty = computed[path];
if(computedProperty){
if(computedProperty.type==='expression' && !computedProperty.get) this._touchExpr(computedProperty);
if(computedProperty.get) return computedProperty.get(this);
else _.log("the computed '" + path + "' don't define the get function, get data."+path + " altnately", "warn")

// reject to get from computed, return undefined directly
// like { empty.prop }, empty equals undefined
// prop shouldn't get from computed
if(parent === null) {
return undefined
}

if(parent && typeof parent[path] !== 'undefined') {
return parent[path]
}

// without parent, get from computed
if (parent !== null) {
var computed = this.computed,
computedProperty = computed[path];
if(computedProperty){
if(computedProperty.type==='expression' && !computedProperty.get) this._touchExpr(computedProperty);
if(computedProperty.get) return computedProperty.get(this);
else _.log("the computed '" + path + "' don't define the get function, get data."+path + " altnately", "warn")
}
}

if( defaults === undefined ){
Expand Down
28 changes: 27 additions & 1 deletion test/spec/browser-bugfix.js
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,32 @@ it('bugfix #50', function(){


})


describe("M 0.6.1", function(){
it('bug #189: computed override', function(){
var App = Regular.extend({
template: '<div ref=data>{ content.title }</div><div ref=computed>{ title }</div><div ref=empty>{ empty.title }</div>',
computed: {
title() {
return 'from_computed'
}
},
});
var app = new App({
data: {
title: 'from_data_title',
content: {
title: 'from_data'
}
}
});
var $data = app.$refs.data;
var $computed = app.$refs.computed;
var $empty = app.$refs.empty;
expect($data.innerHTML).to.equal('from_data');
expect($computed.innerHTML).to.equal('from_computed');
expect($empty.innerHTML).to.equal('');
})
})

})

0 comments on commit 3bbc847

Please sign in to comment.