These are chat archives for canjs/canjs

6th
Mar 2017
Kevin Phillips
@phillipskevin
Mar 06 2017 14:42
@nriesco can you share a JSBin?
lmagarian
@lmagarian
Mar 06 2017 22:49
I have a property on a superMap that needs to load via another API. I'm trying this but know it's not right. Any tips?
"possibleEvents": {
"value": [],
"get": function () {
return Event.getList({}).then( function ( eventList ) {
return eventList;
});
}
},
Nico R.
@nriesco
Mar 06 2017 22:57
@phillipskevin this would be the closest to what I’m trying to do
Chasen Le Hara
@chasenlehara
Mar 06 2017 23:02
@lmagarian You could assign eventList to another property in your view model, so possibleEvents is the promise and eventList is the array.
I personally like to follow this pattern of having one property be the promise: https://github.com/donejs/bitballs/blob/master/public/components/player/list/list.js#L64-L68
Then use the promise’s value in a template, or wherever else: https://github.com/donejs/bitballs/blob/master/public/components/player/list/list.stache#L19
lmagarian
@lmagarian
Mar 06 2017 23:03
Thanks @chasenlehara
Chasen Le Hara
@chasenlehara
Mar 06 2017 23:03
👍
Chasen Le Hara
@chasenlehara
Mar 06 2017 23:12
@nriesco I think the Type is creating a new instance when you’re expecting it to be null.
Also, new Map() is not what you want, but just new Person(), right?
Here’s how I think I would approach what you’re doing: http://jsbin.com/taguluzeku/1/edit?html,js,output
lmagarian
@lmagarian
Mar 06 2017 23:16
@chasenlehara will that make more than one API call if playersPromise.value is referenced in multiple places?
Nico R.
@nriesco
Mar 06 2017 23:27
@chasenlehara thanks, it looks good and it works as I expected. I’ll try to modify my donejs project to see if I can reproduce what you did.
Chasen Le Hara
@chasenlehara
Mar 06 2017 23:49
@lmagarian It shouldn’t; value is only run the first time to get the initial value.
Even if it were in a get, it would only run again if something observable changed, but there’s nothing observable in that function.
Let’s say you did need to make that API call again because the arguments changed. That might look something like:
    orderBy: {
        value: "name"
    },
    playersPromise: {
        get: function(){
            return Player.getList({orderBy: this.orderBy});
        }
    }