Passing objects between Components/Pages












1














Hello Im passing objects between two pages.
I have two pages in Ionic App. The first page has Defect object and sends it to the second page. The second Page receives the object and calls it's methods. Passing objects is done with usage of NavParams, an Ionic core class. Below you can see the receiving of the object. The Defect object.



export class Defect {
public priority: DefectPriority;
public state: DefectState;
public note: string;
public _id: string;
public posX: number;
public posY: number;
public createdAt: number;
public drawingId: string;
public images: DefectImage;

constructor();
constructor(posY?: number, posX?: number, note?: string, defectId?: string, drawingId?: string) {
if (defectId === undefined || defectId === "") {
throw new Error("incorrect defect id");
}
if (posX === undefined || posY === undefined) {
throw new Error("incorrect coordinates");
}
if (drawingId === undefined || drawingId === "") {
throw new Error("incorrect drawingId");
}
if (drawingId === undefined || drawingId === "") {
throw new Error("incorrect drawingId");
}
this.priority = DefectPriority.NORMAL;
this.createdAt = new Date().getTime();
this.state = DefectState.REPORTED;
this._id = defectId;
this.note = note;
this.posX = posX;
this.posY = posY;
this.drawingId = drawingId;
this.images = ;
}

public getPriority() {
return this.priority;
}

setPriority(value: DefectPriority) {
if (!Object.values(DefectPriority).includes(value.toString())) {
throw new Error("incorrect priority")
}
this.priority = value;
}

public changeState(value: DefectState) {
this.state = value;
}

public setNote(note: string) {
this.note = note;
}

generateImageUrl(creatorName: string): DefectImage {
const newUrl = ObjectId.generate() + '-' + this._id + '.jpg';
const defectImage = new DefectImage(newUrl, creatorName, new Date().getMilliseconds());
this.addImage(defectImage);
return defectImage;
}

addImage(defectImage: DefectImage) {
if (!this.images) {
this.images = ;
}
this.images.push(defectImage);
}


Here is the receiving class:



defect: Defect;

constructor(private viewCtrl: ViewController,
private nav: NavParams,
private navCtrl: NavController,
private photo: PhotoProvider) {
this.defect = this.nav.get('defect');
}


Defect class apart from some properties has also methods like: generateImageUrl



Now when I change view to the component where the defect is beeing fetched from params internally it is just JS object without information about Defect class methods: Which means I cannot call methods defined in defect class after I send it to the another Page.



enter image description here



Notice no custom methods like generateImageUrl. Is there a way that I could not lose informations about this object? Or should I just recreate this object from data in the new component ?? my goal on screen below:
enter image description here



the way Im passing data:



const defectModal = this.modalCtrl.create(DefectDetailModal, {
defect: this.defect
});









share|improve this question
























  • It would help if you could add some more details on what exactly it is that you're trying to share. A little bit of context goes a long way.
    – SiddAjmera
    Nov 19 '18 at 17:43










  • did my best. If oyu have more question let me know
    – filemonczyk
    Nov 19 '18 at 17:49










  • Okay so Defect is a class and you're passing an instance of the Defect class as NavParams. Why exactly are you doing that though? Don't you think it's going to have an import on your App Navigation? What exactly is your rationale behind passing such a huge Object?
    – SiddAjmera
    Nov 19 '18 at 17:52










  • I am passing this object so that i can edit it in the new place
    – filemonczyk
    Nov 19 '18 at 17:56










  • can you show the way that you pass defect object?
    – Asanka
    Nov 19 '18 at 18:03
















1














Hello Im passing objects between two pages.
I have two pages in Ionic App. The first page has Defect object and sends it to the second page. The second Page receives the object and calls it's methods. Passing objects is done with usage of NavParams, an Ionic core class. Below you can see the receiving of the object. The Defect object.



export class Defect {
public priority: DefectPriority;
public state: DefectState;
public note: string;
public _id: string;
public posX: number;
public posY: number;
public createdAt: number;
public drawingId: string;
public images: DefectImage;

constructor();
constructor(posY?: number, posX?: number, note?: string, defectId?: string, drawingId?: string) {
if (defectId === undefined || defectId === "") {
throw new Error("incorrect defect id");
}
if (posX === undefined || posY === undefined) {
throw new Error("incorrect coordinates");
}
if (drawingId === undefined || drawingId === "") {
throw new Error("incorrect drawingId");
}
if (drawingId === undefined || drawingId === "") {
throw new Error("incorrect drawingId");
}
this.priority = DefectPriority.NORMAL;
this.createdAt = new Date().getTime();
this.state = DefectState.REPORTED;
this._id = defectId;
this.note = note;
this.posX = posX;
this.posY = posY;
this.drawingId = drawingId;
this.images = ;
}

public getPriority() {
return this.priority;
}

setPriority(value: DefectPriority) {
if (!Object.values(DefectPriority).includes(value.toString())) {
throw new Error("incorrect priority")
}
this.priority = value;
}

public changeState(value: DefectState) {
this.state = value;
}

public setNote(note: string) {
this.note = note;
}

generateImageUrl(creatorName: string): DefectImage {
const newUrl = ObjectId.generate() + '-' + this._id + '.jpg';
const defectImage = new DefectImage(newUrl, creatorName, new Date().getMilliseconds());
this.addImage(defectImage);
return defectImage;
}

addImage(defectImage: DefectImage) {
if (!this.images) {
this.images = ;
}
this.images.push(defectImage);
}


Here is the receiving class:



defect: Defect;

constructor(private viewCtrl: ViewController,
private nav: NavParams,
private navCtrl: NavController,
private photo: PhotoProvider) {
this.defect = this.nav.get('defect');
}


Defect class apart from some properties has also methods like: generateImageUrl



Now when I change view to the component where the defect is beeing fetched from params internally it is just JS object without information about Defect class methods: Which means I cannot call methods defined in defect class after I send it to the another Page.



enter image description here



Notice no custom methods like generateImageUrl. Is there a way that I could not lose informations about this object? Or should I just recreate this object from data in the new component ?? my goal on screen below:
enter image description here



the way Im passing data:



const defectModal = this.modalCtrl.create(DefectDetailModal, {
defect: this.defect
});









share|improve this question
























  • It would help if you could add some more details on what exactly it is that you're trying to share. A little bit of context goes a long way.
    – SiddAjmera
    Nov 19 '18 at 17:43










  • did my best. If oyu have more question let me know
    – filemonczyk
    Nov 19 '18 at 17:49










  • Okay so Defect is a class and you're passing an instance of the Defect class as NavParams. Why exactly are you doing that though? Don't you think it's going to have an import on your App Navigation? What exactly is your rationale behind passing such a huge Object?
    – SiddAjmera
    Nov 19 '18 at 17:52










  • I am passing this object so that i can edit it in the new place
    – filemonczyk
    Nov 19 '18 at 17:56










  • can you show the way that you pass defect object?
    – Asanka
    Nov 19 '18 at 18:03














1












1








1







Hello Im passing objects between two pages.
I have two pages in Ionic App. The first page has Defect object and sends it to the second page. The second Page receives the object and calls it's methods. Passing objects is done with usage of NavParams, an Ionic core class. Below you can see the receiving of the object. The Defect object.



export class Defect {
public priority: DefectPriority;
public state: DefectState;
public note: string;
public _id: string;
public posX: number;
public posY: number;
public createdAt: number;
public drawingId: string;
public images: DefectImage;

constructor();
constructor(posY?: number, posX?: number, note?: string, defectId?: string, drawingId?: string) {
if (defectId === undefined || defectId === "") {
throw new Error("incorrect defect id");
}
if (posX === undefined || posY === undefined) {
throw new Error("incorrect coordinates");
}
if (drawingId === undefined || drawingId === "") {
throw new Error("incorrect drawingId");
}
if (drawingId === undefined || drawingId === "") {
throw new Error("incorrect drawingId");
}
this.priority = DefectPriority.NORMAL;
this.createdAt = new Date().getTime();
this.state = DefectState.REPORTED;
this._id = defectId;
this.note = note;
this.posX = posX;
this.posY = posY;
this.drawingId = drawingId;
this.images = ;
}

public getPriority() {
return this.priority;
}

setPriority(value: DefectPriority) {
if (!Object.values(DefectPriority).includes(value.toString())) {
throw new Error("incorrect priority")
}
this.priority = value;
}

public changeState(value: DefectState) {
this.state = value;
}

public setNote(note: string) {
this.note = note;
}

generateImageUrl(creatorName: string): DefectImage {
const newUrl = ObjectId.generate() + '-' + this._id + '.jpg';
const defectImage = new DefectImage(newUrl, creatorName, new Date().getMilliseconds());
this.addImage(defectImage);
return defectImage;
}

addImage(defectImage: DefectImage) {
if (!this.images) {
this.images = ;
}
this.images.push(defectImage);
}


Here is the receiving class:



defect: Defect;

constructor(private viewCtrl: ViewController,
private nav: NavParams,
private navCtrl: NavController,
private photo: PhotoProvider) {
this.defect = this.nav.get('defect');
}


Defect class apart from some properties has also methods like: generateImageUrl



Now when I change view to the component where the defect is beeing fetched from params internally it is just JS object without information about Defect class methods: Which means I cannot call methods defined in defect class after I send it to the another Page.



enter image description here



Notice no custom methods like generateImageUrl. Is there a way that I could not lose informations about this object? Or should I just recreate this object from data in the new component ?? my goal on screen below:
enter image description here



the way Im passing data:



const defectModal = this.modalCtrl.create(DefectDetailModal, {
defect: this.defect
});









share|improve this question















Hello Im passing objects between two pages.
I have two pages in Ionic App. The first page has Defect object and sends it to the second page. The second Page receives the object and calls it's methods. Passing objects is done with usage of NavParams, an Ionic core class. Below you can see the receiving of the object. The Defect object.



export class Defect {
public priority: DefectPriority;
public state: DefectState;
public note: string;
public _id: string;
public posX: number;
public posY: number;
public createdAt: number;
public drawingId: string;
public images: DefectImage;

constructor();
constructor(posY?: number, posX?: number, note?: string, defectId?: string, drawingId?: string) {
if (defectId === undefined || defectId === "") {
throw new Error("incorrect defect id");
}
if (posX === undefined || posY === undefined) {
throw new Error("incorrect coordinates");
}
if (drawingId === undefined || drawingId === "") {
throw new Error("incorrect drawingId");
}
if (drawingId === undefined || drawingId === "") {
throw new Error("incorrect drawingId");
}
this.priority = DefectPriority.NORMAL;
this.createdAt = new Date().getTime();
this.state = DefectState.REPORTED;
this._id = defectId;
this.note = note;
this.posX = posX;
this.posY = posY;
this.drawingId = drawingId;
this.images = ;
}

public getPriority() {
return this.priority;
}

setPriority(value: DefectPriority) {
if (!Object.values(DefectPriority).includes(value.toString())) {
throw new Error("incorrect priority")
}
this.priority = value;
}

public changeState(value: DefectState) {
this.state = value;
}

public setNote(note: string) {
this.note = note;
}

generateImageUrl(creatorName: string): DefectImage {
const newUrl = ObjectId.generate() + '-' + this._id + '.jpg';
const defectImage = new DefectImage(newUrl, creatorName, new Date().getMilliseconds());
this.addImage(defectImage);
return defectImage;
}

addImage(defectImage: DefectImage) {
if (!this.images) {
this.images = ;
}
this.images.push(defectImage);
}


Here is the receiving class:



defect: Defect;

constructor(private viewCtrl: ViewController,
private nav: NavParams,
private navCtrl: NavController,
private photo: PhotoProvider) {
this.defect = this.nav.get('defect');
}


Defect class apart from some properties has also methods like: generateImageUrl



Now when I change view to the component where the defect is beeing fetched from params internally it is just JS object without information about Defect class methods: Which means I cannot call methods defined in defect class after I send it to the another Page.



enter image description here



Notice no custom methods like generateImageUrl. Is there a way that I could not lose informations about this object? Or should I just recreate this object from data in the new component ?? my goal on screen below:
enter image description here



the way Im passing data:



const defectModal = this.modalCtrl.create(DefectDetailModal, {
defect: this.defect
});






javascript angular typescript ionic-framework angular6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 18:22

























asked Nov 19 '18 at 17:37









filemonczyk

354321




354321












  • It would help if you could add some more details on what exactly it is that you're trying to share. A little bit of context goes a long way.
    – SiddAjmera
    Nov 19 '18 at 17:43










  • did my best. If oyu have more question let me know
    – filemonczyk
    Nov 19 '18 at 17:49










  • Okay so Defect is a class and you're passing an instance of the Defect class as NavParams. Why exactly are you doing that though? Don't you think it's going to have an import on your App Navigation? What exactly is your rationale behind passing such a huge Object?
    – SiddAjmera
    Nov 19 '18 at 17:52










  • I am passing this object so that i can edit it in the new place
    – filemonczyk
    Nov 19 '18 at 17:56










  • can you show the way that you pass defect object?
    – Asanka
    Nov 19 '18 at 18:03


















  • It would help if you could add some more details on what exactly it is that you're trying to share. A little bit of context goes a long way.
    – SiddAjmera
    Nov 19 '18 at 17:43










  • did my best. If oyu have more question let me know
    – filemonczyk
    Nov 19 '18 at 17:49










  • Okay so Defect is a class and you're passing an instance of the Defect class as NavParams. Why exactly are you doing that though? Don't you think it's going to have an import on your App Navigation? What exactly is your rationale behind passing such a huge Object?
    – SiddAjmera
    Nov 19 '18 at 17:52










  • I am passing this object so that i can edit it in the new place
    – filemonczyk
    Nov 19 '18 at 17:56










  • can you show the way that you pass defect object?
    – Asanka
    Nov 19 '18 at 18:03
















It would help if you could add some more details on what exactly it is that you're trying to share. A little bit of context goes a long way.
– SiddAjmera
Nov 19 '18 at 17:43




It would help if you could add some more details on what exactly it is that you're trying to share. A little bit of context goes a long way.
– SiddAjmera
Nov 19 '18 at 17:43












did my best. If oyu have more question let me know
– filemonczyk
Nov 19 '18 at 17:49




did my best. If oyu have more question let me know
– filemonczyk
Nov 19 '18 at 17:49












Okay so Defect is a class and you're passing an instance of the Defect class as NavParams. Why exactly are you doing that though? Don't you think it's going to have an import on your App Navigation? What exactly is your rationale behind passing such a huge Object?
– SiddAjmera
Nov 19 '18 at 17:52




Okay so Defect is a class and you're passing an instance of the Defect class as NavParams. Why exactly are you doing that though? Don't you think it's going to have an import on your App Navigation? What exactly is your rationale behind passing such a huge Object?
– SiddAjmera
Nov 19 '18 at 17:52












I am passing this object so that i can edit it in the new place
– filemonczyk
Nov 19 '18 at 17:56




I am passing this object so that i can edit it in the new place
– filemonczyk
Nov 19 '18 at 17:56












can you show the way that you pass defect object?
– Asanka
Nov 19 '18 at 18:03




can you show the way that you pass defect object?
– Asanka
Nov 19 '18 at 18:03












1 Answer
1






active

oldest

votes


















1














I'm assuming that Defect is an entity in your App. Angular's Style Guide recommends using interfaces for data models instead of classes.




Consider using an interface for data models.




That being said, you should have created a DefectService, where you would have set some property for the current defect that you're dealing with.



You could have then injected the service in the components that you wanted to share data between. Then from one component, you could have set the defect and then you could get the defect in the other component using setters and getters.






share|improve this answer





















  • I think good solution here could be to use behaviour subject and then get it from there, would work like service you describe
    – filemonczyk
    Nov 19 '18 at 18:39










  • Definitely. That would work as well.
    – SiddAjmera
    Nov 19 '18 at 18:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379956%2fpassing-objects-between-components-pages%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I'm assuming that Defect is an entity in your App. Angular's Style Guide recommends using interfaces for data models instead of classes.




Consider using an interface for data models.




That being said, you should have created a DefectService, where you would have set some property for the current defect that you're dealing with.



You could have then injected the service in the components that you wanted to share data between. Then from one component, you could have set the defect and then you could get the defect in the other component using setters and getters.






share|improve this answer





















  • I think good solution here could be to use behaviour subject and then get it from there, would work like service you describe
    – filemonczyk
    Nov 19 '18 at 18:39










  • Definitely. That would work as well.
    – SiddAjmera
    Nov 19 '18 at 18:40
















1














I'm assuming that Defect is an entity in your App. Angular's Style Guide recommends using interfaces for data models instead of classes.




Consider using an interface for data models.




That being said, you should have created a DefectService, where you would have set some property for the current defect that you're dealing with.



You could have then injected the service in the components that you wanted to share data between. Then from one component, you could have set the defect and then you could get the defect in the other component using setters and getters.






share|improve this answer





















  • I think good solution here could be to use behaviour subject and then get it from there, would work like service you describe
    – filemonczyk
    Nov 19 '18 at 18:39










  • Definitely. That would work as well.
    – SiddAjmera
    Nov 19 '18 at 18:40














1












1








1






I'm assuming that Defect is an entity in your App. Angular's Style Guide recommends using interfaces for data models instead of classes.




Consider using an interface for data models.




That being said, you should have created a DefectService, where you would have set some property for the current defect that you're dealing with.



You could have then injected the service in the components that you wanted to share data between. Then from one component, you could have set the defect and then you could get the defect in the other component using setters and getters.






share|improve this answer












I'm assuming that Defect is an entity in your App. Angular's Style Guide recommends using interfaces for data models instead of classes.




Consider using an interface for data models.




That being said, you should have created a DefectService, where you would have set some property for the current defect that you're dealing with.



You could have then injected the service in the components that you wanted to share data between. Then from one component, you could have set the defect and then you could get the defect in the other component using setters and getters.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 18:35









SiddAjmera

13.1k31137




13.1k31137












  • I think good solution here could be to use behaviour subject and then get it from there, would work like service you describe
    – filemonczyk
    Nov 19 '18 at 18:39










  • Definitely. That would work as well.
    – SiddAjmera
    Nov 19 '18 at 18:40


















  • I think good solution here could be to use behaviour subject and then get it from there, would work like service you describe
    – filemonczyk
    Nov 19 '18 at 18:39










  • Definitely. That would work as well.
    – SiddAjmera
    Nov 19 '18 at 18:40
















I think good solution here could be to use behaviour subject and then get it from there, would work like service you describe
– filemonczyk
Nov 19 '18 at 18:39




I think good solution here could be to use behaviour subject and then get it from there, would work like service you describe
– filemonczyk
Nov 19 '18 at 18:39












Definitely. That would work as well.
– SiddAjmera
Nov 19 '18 at 18:40




Definitely. That would work as well.
– SiddAjmera
Nov 19 '18 at 18:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379956%2fpassing-objects-between-components-pages%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory