[SalesForce] Attribute Lightning Component

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

Best Answer

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.

Related Topic