4

I'm trying to load an item within my GUI extension but keep the item in the properties for use later, but i'm having an issue storing the item inside a property array.

My code is farmed out into a 'worker' class

Here's my code to construct and set up my properties array (note the property p.currentItem)

Namespace.ToolWorker = function ()
{
    this.properties = [];
    var p = this.properties;
    p.itemId = $url.getHashParam("id");
    p.currentItem = null;

};

and in my set up method, I load the object like this:

Namespace.ToolWorker.prototype._setupContent = function() {
    $log.message("Setting up content");
    var p = this.properties;

    var currentItem = $models.getItem(p.itemId);

    function itemLoaded() {
        $evt.removeEventHandler(currentItem, "load", itemLoaded);
        p.currentItem = currentItem;
    }
    $evt.addEventHandler(currentItem, "load", itemLoaded);

    currentItem.load();
    $j('#currentItemContent').html(p.currentItem.getXml());
}

So I store the variable by saying p.currentItem = currentItem; but i'm seeing that when using it outside of the itemLoaded() the object is null.

It's been a while and i'm likely doing something silly :)

6

There are few things:

  1. Your last string in which you access p.currentItem executes before itemLoaded() callback. Thus p.currentItem is not defined yet.
  2. Storing model item in local variable is anti-pattern. You should never do this. Instead you can store item's ID and always do $models.getItem([itemID]) to access item. (This is related to built-in marshaling mechanism).

So your code should look like this:

Namespace.ToolWorker.prototype._setupContent = function() {
    $log.message("Setting up content");
    var p = this.properties;

    function itemLoaded(e) {
        var item = $models.getItem(p.itemId);

        if(e) {
            $evt.removeEventHandler(item, "load", itemLoaded);
        }
        $j('#currentItemContent').html(item.getXml());
    }

    var currentItem = $models.getItem(p.itemId);
    if(currentItem.isLoaded(true)) {
        itemLoaded();
    } else {
        $evt.addEventHandler(currentItem, "load", itemLoaded);
        currentItem.load(true);
    }
}

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.