I was looking at this link and it mentions to put INIT(); into the object so when ever you create the object it would auto init();
http://oreilly.com/pub/a/javascript/2003/09/24/coldfusion_tips.html
So if I do this below as my employeeService and
/*
- Employee Service
-
@accessors true
*/
component employeeService output=“false” {
init();
/*
- init
*/
function init() output=“false” {
var oEmployee = createObject(‘component’, ‘testObjects.model.employee.employee’).init();
return oEmployee;
}
}
My Employee Object
/*
- Employee
-
@accessors true
/
component employee output=“false” extends=“testObjects.model.user.user” {
/ properties */
property name=“rank” type=“string”;
/*
- init
*/
function init() output=“false” {
super.init();
//department=“HR”;
return this;
}
}
and then in my test page put this call to it.
session.oEmployeeService = createObject('component', 'testObjects.model.employee.employeeService');
Am I misunderstanding or shouldn’t it auto init?
I even copied their code.
<cfset init()>
<cfset session.myCart = createObject(“component”,“testObjects.model.shoppingCart”)>
It still does not seem to work.
The init() does not work with CreateObject.
Also in ColdFusion 9 when doing myObject - new component(); it will auto init and there is no need for the init()…
Hope that helps.
Sorry I should have said auto init() doesn’t work with CreateObject
Okay I made changes and I am getting closer What am I doing wrong now?
I want to initialize the employeeService which then will give me the employee Object with all of the extended properties?
session.oEmployeeService = new model.employee.employeeService();
What is the problem?
Your code should dump the object that was returned… You not seeing this?
Your init() method should return a reference to the object instance
being initialized, i.e. 'this'. So:
function init() {
var oPerson = new model.person.person();
return this;
}
NOT:
function init() {
var oPerson = new model.person.person();
return oPerson;
}
I am trying to get it where when I create employee it extends, user and user Extends person.
so if I have in employee property department and in user property userId and in person property personId…
Then when I do
session.oEmployeeService = new model.employee.employeeService();
and I dump the session, I should see the object with all three properties.
also when using
var oPerson = new model.person.person();
Not only dont you need the init method, but the return this is also not needed.
This is ColdFusion 9 only too.