These are chat archives for canjs/canjs

25th
Aug 2016
Thomas Sieverding
@Bajix
Aug 25 2016 01:39
@justinbmeyer Is there any mechanism to change listSet? I’ve got a use-case in which I want dynamic subsets of items in my instanceStore
Thomas Sieverding
@Bajix
Aug 25 2016 03:44
@matthewp isn’t that just a static prop?
I need to be able to change the set logic and get new results
And I could be wrong, but does the timing of that work to add in pre-existing items?
Thomas Sieverding
@Bajix
Aug 25 2016 03:50
Ideally I’d have a getter that reads other props and updated my set logic accordingly
Thomas Sieverding
@Bajix
Aug 25 2016 06:29
This message was deleted
Christopher Oliphant
@RALifeCoach
Aug 25 2016 10:56
@matthewp karma-steal-npm. I have worked my way around the issue loading the guid, now I am stuck on transpiling. My code is written in ES6 and the coverage preparser doesn’t know what to do with the import statements. Looking at the karma-steal-npm code, it appears that it doesn’t transpile the files.
Christopher Oliphant
@RALifeCoach
Aug 25 2016 18:29
I am learning about promises and can-connect. I want to read 4 different json files within a can-connect.getData. I have defined 4 sub-models each as a can-connect for a single file. I then string the four subModel.get’s together in a $.when. The $.when is returning promises, not the data from the get.
Here is my getData for the model
    getData: function (params) {
        this.countryCode = params.countryCode;
        var playerBio = PlayerBio.get({
            options: params.options,
            id: params.id
        });
        var playerStats = PlayerStats.get({
            options: params.options,
            id: params.id,
            year: params.year
        });
        var playerStat186 = PlayerStat186.get({
            options: params.options,
            id: params.id
        });
        var playerResults = PlayerResults.get({
            options: params.options,
            id: params.id,
            year: params.year
        });
        return new Promise((resolve, reject)=>{
            $.when(playerBio, playerStats, playerStat186, playerResults)
            .then((dataBio, dataStat, dataStat186, dataResults)=>{
                resolve({
                    dataBio: dataBio,
                    dataStat: dataStat,
                    dataStat186: dataStat186,
                    dataResults: dataResults
                });
            }, reject);
        });
Here is a sample sub-model:
connect([
    connectConstructor,
    connectMap,
    connectDataParse,
    connectStore
], {
    Map: PlayerBio,
    getData: function (params){
        var url = Utils.format(params.options.playerBioUrl, {
            id: params.id
        });
        return new Promise((resolve, reject)=>{
            $.get(url).then(resolve, reject);
        });
    },
    parseInstanceData: function (data) {
        var personalInfo = data.plrs[0].personalInfo;
        return {
            playerId: data.plrs[0].plrNum,
            name: personalInfo.name.first + ' ' + personalInfo.name.last,
            height: personalInfo.height,
            heightMetric: personalInfo.heightMetric,
            weight: personalInfo.weight,
            weightMetric: personalInfo.weightMetric,
            age: personalInfo.age,
            school: personalInfo.education,
            birthPlace: personalInfo.birthPlace,
            turnedPro: personalInfo.trndProYear
        };
    }
});
Kevin Phillips
@phillipskevin
Aug 25 2016 19:24
@RALifeCoach have you tried using Promise.all?
Christopher Oliphant
@RALifeCoach
Aug 25 2016 19:25
I will try that instead of $.when
Thomas Sieverding
@Bajix
Aug 25 2016 19:26
@RALifeCoach jQuery deferred/when isn’t Promise A/+ compliant prior to 3.x
Christopher Oliphant
@RALifeCoach
Aug 25 2016 19:26
That’s good to know
Thomas Sieverding
@Bajix
Aug 25 2016 19:26
The easy fix is to use something like Q
You can do type: Q to convert promises
Or do something like Q(can.ajax({ … })
Q.all
Also your example is improper. You should do Q.when -> then directly, rather than nesting it in a new Promise
Christopher Oliphant
@RALifeCoach
Aug 25 2016 19:28
I want to use as must of CanJS as possible. I had $.get’s in my $.when and that was working, but I wanted the CanJS features for parsing the data when it arrives
Thomas Sieverding
@Bajix
Aug 25 2016 19:29
$.get(url) returns a promise, so you wouldn’t need wrap that either
You could do $.get(url).then or otherwise Q($.get(url)).then
They’re different issues
Christopher Oliphant
@RALifeCoach
Aug 25 2016 19:29
I do $.get(url) in the sub-models
Thomas Sieverding
@Bajix
Aug 25 2016 19:30
Good
Christopher Oliphant
@RALifeCoach
Aug 25 2016 19:30
But I also need to return a Promise from my main model
Thomas Sieverding
@Bajix
Aug 25 2016 19:30
$.get is a promise
You don’t need to do new Promise(function( resolve, reject) { … })
Christopher Oliphant
@RALifeCoach
Aug 25 2016 19:31
yes, I know, but the main model needs to return a promise as well as the sub-models
Thomas Sieverding
@Bajix
Aug 25 2016 19:32
It does
$.get is a promise. You can do return $.get instead of wrapping it in new Promise
Christopher Oliphant
@RALifeCoach
Aug 25 2016 19:33
But I have 4 $.gets that I want completed before returning from the main model
Thomas Sieverding
@Bajix
Aug 25 2016 19:33
If you have issues with promise compatibility, then wrap it in Q
You’re not following. These are identical:
return new Promise((resolve, reject)=>{
  $.get(url).then(resolve, reject);
});
return $.get(url);
Christopher Oliphant
@RALifeCoach
Aug 25 2016 19:35
Ah, in the sub-models, I see - (I think I took that from the CanJS docs)
Thomas Sieverding
@Bajix
Aug 25 2016 19:36
That’s a compatability conversion and nothing else
You usually won’t have any issues with compatibility, but if you do, then utilize a dedicated promise library to sort out the inconsistencies
Hence Q($.get(url)).then
Christopher Oliphant
@RALifeCoach
Aug 25 2016 19:37
This message was deleted
Christopher Oliphant
@RALifeCoach
Aug 25 2016 19:42
The problem turned out to be an undefined variable. The error message was swallowed by CanJS.
And my code is simpler now - thanks
I think that what you guys have done with CanJS is truly amazing. It would be easier if there was better error capturing and reporting, but the way the pieces hang together is amazing.
Thomas Sieverding
@Bajix
Aug 25 2016 19:48
That’s just sort of par for the course development
The errors are also not swallowed
Christopher Oliphant
@RALifeCoach
Aug 25 2016 19:48
It works without the promise conversions. Quite nice!
Kevin Phillips
@phillipskevin
Aug 25 2016 19:48
if there are specific errors (like that one) that get swallowed, open an issue
and we can work on the reporting
Christopher Oliphant
@RALifeCoach
Aug 25 2016 19:57
I have created issue canjs/can-connect#136
qantourisc
@qantourisc
Aug 25 2016 20:01
Is there a way to not have findAll convert the data to a model ? Since I wish to convert it myself, due to some members that need data-convertion
"Could not get any raw data while converint using .models"
Matthew Phillips
@matthewp
Aug 25 2016 20:02
@qantourisc , no but you can use $.ajax directly, might be what you want.
qantourisc @qantourisc double checks
qantourisc
@qantourisc
Aug 25 2016 20:06
A ok I see, so I should implement a function not named findAll
Rather anoying that some models would have a differnt api then
@matthewp ok "found it" I was retuning an object not a list. If you return a list of models, canjs accepts this input. So no need, I just had to implement my converter correctly.