Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite3e0914

Browse files
kakato10jmdobry
authored andcommitted
Added support for computed properties with property accessors. (#13)
1 parent39a2488 commite3e0914

File tree

5 files changed

+69
-33
lines changed

5 files changed

+69
-33
lines changed

‎dist/js-data-angular-mocks.js‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -402,23 +402,30 @@ return /******/ (function(modules) { // webpackBootstrap
402402
def.errorFn('Computed property "'+field+'" conflicts with previously defined prototype method!');
403403
}
404404
def.omit.push(field);
405-
vardeps;
406-
if(fn.length===1){
407-
varmatch=fn[0].toString().match(/function.*?\(([\s\S]*?)\)/);
408-
deps=match[1].split(',');
409-
def.computed[field]=deps.concat(fn);
410-
fn=def.computed[field];
411-
if(deps.length){
412-
def.errorFn('Use the computed property array syntax for compatibility with minified code!');
405+
if(DSUtils.isArray(fn)){
406+
vardeps;
407+
if(fn.length===1){
408+
varmatch=fn[0].toString().match(/function.*?\(([\s\S]*?)\)/);
409+
deps=match[1].split(',');
410+
deps=DSUtils.filter(deps,function(dep){
411+
return!!dep;
412+
});
413+
def.computed[field]=deps.concat(fn);
414+
fn=def.computed[field];
415+
if(deps.length){
416+
def.errorFn('Use the computed property array syntax for compatibility with minified code!');
417+
}
413418
}
419+
deps=fn.slice(0,fn.length-1);
420+
DSUtils.forEach(deps,function(val,index){
421+
deps[index]=val.trim();
422+
});
423+
fn.deps=DSUtils.filter(deps,function(dep){
424+
return!!dep;
425+
});
426+
}elseif(DSUtils.isObject(fn)){
427+
Object.defineProperty(def[_class].prototype,field,fn);
414428
}
415-
deps=fn.slice(0,fn.length-1);
416-
DSUtils.forEach(deps,function(val,index){
417-
deps[index]=val.trim();
418-
});
419-
fn.deps=DSUtils.filter(deps,function(dep){
420-
return!!dep;
421-
});
422429
});
423430

424431
// add instance proxies of DS methods

‎dist/js-data-angular-mocks.min.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎dist/js-data-angular-mocks.min.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎src/index.js‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -362,23 +362,30 @@ class DS {
362362
def.errorFn(`Computed property "${field}" conflicts with previously defined prototype method!`);
363363
}
364364
def.omit.push(field);
365-
vardeps;
366-
if(fn.length===1){
367-
letmatch=fn[0].toString().match(/function.*?\(([\s\S]*?)\)/);
368-
deps=match[1].split(',');
369-
def.computed[field]=deps.concat(fn);
370-
fn=def.computed[field];
371-
if(deps.length){
372-
def.errorFn('Use the computed property array syntax for compatibility with minified code!');
365+
if(DSUtils.isArray(fn)){
366+
vardeps;
367+
if(fn.length===1){
368+
letmatch=fn[0].toString().match(/function.*?\(([\s\S]*?)\)/);
369+
deps=match[1].split(',');
370+
deps=DSUtils.filter(deps,dep=>{
371+
return!!dep;
372+
});
373+
def.computed[field]=deps.concat(fn);
374+
fn=def.computed[field];
375+
if(deps.length){
376+
def.errorFn('Use the computed property array syntax for compatibility with minified code!');
377+
}
373378
}
379+
deps=fn.slice(0,fn.length-1);
380+
DSUtils.forEach(deps,(val,index)=>{
381+
deps[index]=val.trim();
382+
});
383+
fn.deps=DSUtils.filter(deps,dep=>{
384+
return!!dep;
385+
});
386+
}elseif(DSUtils.isObject(fn)){
387+
Object.defineProperty(def[_class].prototype,field,fn);
374388
}
375-
deps=fn.slice(0,fn.length-1);
376-
DSUtils.forEach(deps,(val,index)=>{
377-
deps[index]=val.trim();
378-
});
379-
fn.deps=DSUtils.filter(deps,dep=>{
380-
return!!dep;
381-
});
382389
});
383390

384391
// add instance proxies of DS methods

‎test/testApp.js‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
angular.module('testApp',['js-data'])
22
.service('User',function(DS){
3-
returnDS.defineResource('user');
3+
returnDS.defineResource({
4+
name:'user',
5+
computed:{
6+
fullName:{
7+
get:function(){
8+
return'John Doe';
9+
}
10+
},
11+
nickname:function(){
12+
return'Johny';
13+
},
14+
alternateNickname:[function(){
15+
return'Doey';
16+
}],
17+
initials:['fullName',function(fullName){
18+
varnames=fullName.split(' ');
19+
varinitials=names.reduce(function(initials,name){
20+
returninitials+name[0].toUpperCase();
21+
},'')
22+
returninitials;
23+
}]
24+
}
25+
});
426
})
527
.controller('TestCtrl',function($scope,DS,User){
628
'use strict';

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp