3

Long story short : I tried to send an attribute A1 with a lookup from a component C1 to a component C2. And when component C2 is rendered, the lookup from A1 just isn't there.

The idea is to allow the user to edit an item and when he cancels the edit the previous values are restored. As I'm on a standalone app, I canno't make use of e.force:editRecord from Sf1.

So when the user uses the edit and cancels it, the item keeps the values he puts in the edit in the first place so it could be bad for the UX as the user will see the changes from an item he didn't want to save.

See the code here C1.cmp

    <aura:attribute name="item" type="Item__c"/>
    <aura:attribute name="oldItem" type="Item__c"/>

C1Controller.js

 doInit : function(component, event, helper) {
    component.set("v.oldItem", component.get("v.item"));
}
navToTaskEdit : function(component, event) {
   //...
   var destination = "markup://c:itemEdit";
        $A.componentService.newComponentAsync(this, function(item) {
            var content = component.find("content");
            content.set("v.body", item);
        }, {
            componentDef: destination,
            attributes: {
                values: {
                    item: "{!v.item}",
                    oldItem: "{!v.oldItem}"
                }
            }
        }, component);

C2.cmp

<aura:attribute name="item" type="Item__c"/>
<aura:attribute name="oldItem" type="Item__c"/>

C2Controller.js

    var oldItem = component.get('v.oldItem');
    console.log('i '+oldItem.Name);
    console.log('i2 '+oldItem.Lookup__c);

So the name is displayed but the lookup isn't. I checked in the debugger from the chrome dev tool and all the fields were in the oldItem except the lookup.

Any ideas ?

Thanks in advance

2

Without the full code sample it's tough to tell why Lookup__c field isn't present, but I'm betting it's because of object references.

This bit here

component.set("v.oldItem", component.get("v.item"));

Associates the v.oldItem object with a reference (in javascript not Lightning) to the v.item object.

Since you said you wanted v.oldItem not to change when v.item changed, I'm betting this is not what you wanted.

Try doing this as a cheap and dirty copy.

component.set("v.oldItem", JSON.parse(JSON.stringify(component.get("v.item"))));

See if that works.

We do not clone objects in Lightning, that would get messy perf wise, so primitives are done by value, objects (and arrays) are done by reference.

  • Works like a charm. I canno't talk about the perf wise, but I'm interested in why would it be a bad for the perf wise. Having a Java background, I may be biased : I miss some clone() method for objects and arrays :) Thanks again ! – Simon Govaert May 19 '15 at 12:01
  • First, we want to get closer to acting like normal Javascript, so you should know what to expect in most cases. Second, imagine you pass in something like, cmp.set("v.domRef", document); that would be very expensive to clone. What if you didn't want a clone? Cloning is a recursive loop that just runs amuck. – Kris Gray May 19 '15 at 14:12
  • Awesome! It worked for my project's use case too.. Thanks :) – Monalisa Das Jul 4 '18 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.