Add make 'lazy' function a service

pull/4238/head
Alex Dik 9 years ago
parent 3daf551e34
commit 44cec93e89
  1. 4
      frontend/app/components/api/api-v3/hal/hal-resource.service.ts
  2. 102
      frontend/app/components/common/lazy/lazy.service.test.ts
  3. 60
      frontend/app/components/common/lazy/lazy.service.ts
  4. 32
      frontend/app/components/open-project.module.ts
  5. 2
      frontend/app/openproject-app.js

@ -26,9 +26,7 @@
// See doc/COPYRIGHT.rdoc for more details.
//++
import {lazy} from './../../../open-project.module';
function halResource($q, _, halTransform, HalLink) {
function halResource($q, _, lazy, halTransform, HalLink) {
return class HalResource {
protected static fromLink(link) {
return new HalResource({_links: {self: link}}, false);

@ -0,0 +1,102 @@
// -- copyright
// OpenProject is a project management system.
// Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License version 3.
//
// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
// Copyright (C) 2006-2013 Jean-Philippe Lang
// Copyright (C) 2010-2013 the ChiliProject Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// See doc/COPYRIGHT.rdoc for more details.
// ++
describe('lazy service', () => {
var lazy;
beforeEach(angular.mock.module('openproject.services'));
beforeEach(angular.mock.inject(_lazy_ => {
lazy = _lazy_;
}));
it('should exist', () => {
expect(lazy).to.exist;
});
it('should add a property with the given name to the object', () => {
let obj = {
prop: void 0
};
lazy(obj, 'prop', () => '');
expect(obj.prop).to.exist;
});
it('should add an enumerable property', () => {
let obj = {
prop: void 0
};
lazy(obj, 'prop', () => '');
expect(obj.propertyIsEnumerable('prop')).to.be.true;
});
it('should add a configurable property', () => {
let obj = {
prop: void 0
};
lazy(obj, 'prop', () => '');
expect(Object.getOwnPropertyDescriptor(obj, 'prop').configurable).to.be.true;
});
it('should set the value of the property provided by the setter', () => {
let obj = {
prop: void 0
};
lazy(obj, 'prop', () => '', val => val);
obj.prop = 'hello';
expect(obj.prop).to.eq('hello');
});
it('should not be settable, if no setter is provided', () => {
let obj = {
prop: void 0
};
lazy(obj, 'prop', () => '');
try {
obj.prop = 'hello';
}
catch (Error) {}
expect(obj.prop).to.not.eq('hello');
});
it('should do nothing if the target is not an object', () => {
let obj = null;
lazy(obj, 'prop', () => '');
expect(obj).to.not.be.ok;
});
it('should call the getter only once', () => {
let callback = sinon.spy();
let obj = {
prop: void 0
};
lazy(obj, 'prop', callback);
obj.prop;
obj.prop;
expect(callback.calledOnce).to.be.true;
});
});

@ -0,0 +1,60 @@
// -- copyright
// OpenProject is a project management system.
// Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License version 3.
//
// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
// Copyright (C) 2006-2013 Jean-Philippe Lang
// Copyright (C) 2010-2013 the ChiliProject Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// See doc/COPYRIGHT.rdoc for more details.
// ++
function lazy (obj:any, property:string, getter:{():any}, setter?:{(value:any)}) {
if (angular.isObject(obj)) {
let done = false;
let value;
let config = {
get() {
if (!done) {
value = getter();
done = true;
}
return value;
},
set: void 0,
configurable: true,
enumerable: true
};
if (setter) {
config.set = val => {
value = setter(val);
done = true;
};
}
Object.defineProperty(obj, property, config);
}
}
angular
.module('openproject.services')
.factory('lazy', () => lazy);

@ -30,35 +30,3 @@ export function opDirective(directive:ng.IDirective = {}, config:ng.IDirective =
// TODO: Replace '_.merge' with AngularJS v1.4 'angular.merge' method
return _.merge(directive, config);
}
export function lazy (obj:any,
property:string,
getter:{():any},
setter?:{(value:any):any}) {
if (angular.isObject(obj)) {
let done = false;
let value;
let config = {
get() {
if (!done) {
value = getter();
done = true;
}
return value;
},
set: void 0,
configurable: true,
enumerable: true
};
if (setter) {
config.set = val => {
value = angular.isFunction(setter) ? setter(val) : val;
}
}
Object.defineProperty(obj, property, config);
}
}

@ -170,7 +170,7 @@ angular.module('openproject.layout', [
]);
angular.module('openproject.layout.controllers', []);
angular.module('openproject.api', ['restangular']);
angular.module('openproject.api', ['restangular', 'openproject.services']);
angular.module('openproject.templates', []);

Loading…
Cancel
Save