These are chat archives for canjs/canjs

12th
Jul 2016
Danil Radkovsky
@Krakabek
Jul 12 2016 13:15
Hello
Is there a way to bind to changes of cookies?
Matthew Phillips
@matthewp
Jul 12 2016 14:06
you could create a compute that did that
something like
(making an example)

var makeCookieCompute = function(){
    var updated = function(){};
    var cookie = document.cookie;

    Object.defineProperty(document, "cookie", {
        get: function(){
            return cookie;
        },
        set: function(val){
            cookie = val;
            updated(val);
        }
    });

    var cookieCompute = can.compute(document.cookie, {
        get: function(){
            return document.cookie;
        },
        set: function(newVal){
            document.cookie = newVal;
        },
        on: function(onupdated){
            updated = onupdated;
        },
        off: function(){
            // TODO unbind the defineProp maybe?
        }
    });

    return cookieCompute;
};
Matthew Phillips
@matthewp
Jul 12 2016 14:11
that's a starting point, can make a better version from there maybe
Danil Radkovsky
@Krakabek
Jul 12 2016 15:07
Thanks!
Sunil George
@georgesunil81
Jul 12 2016 17:48
Help please!!! My canjs. models that were working fine are now giving the error message "Error: Could not get any raw data while converting using .models". Anybody face this before? Any solution?
Kevin Phillips
@phillipskevin
Jul 12 2016 17:50
did the data change?
Sunil George
@georgesunil81
Jul 12 2016 17:51
I am not even able to look at the data! Doesn't give me the dump.
Thats the error trace!
When does this error typically happen? Any hints?
Kevin Phillips
@phillipskevin
Jul 12 2016 17:53
you can’t see the data in the network panel?
Sunil George
@georgesunil81
Jul 12 2016 17:54
Ah, let me see.
Yes, I see the data in the network panel!
Kevin Phillips
@phillipskevin
Jul 12 2016 17:54
I think that error is indicating it’s not an array
Sunil George
@georgesunil81
Jul 12 2016 17:57
Yep, you are right, earlier it was returning an array of objects!
Now it is returning an object, and the array of objects is inside it!
How to handle this now?
Any solution to this Kevin?
The backend needs to send me an object now. The array I am interested in is now within an object called "data"!
So the response that I am getting is like -
{ "isSuccessful": true, "message": "Successfully retrieved the items with the selected criteria!", "data": { "totalNumberOfItems": 57, "items": [{item:1}, {item:2}] } }
Sunil George
@georgesunil81
Jul 12 2016 18:03
The response earlier (when it was working fine) was as below -
{
  "isSuccessful": true,
  "message": "Successfully retrieved the items with the selected criteria!",
  "data": [{item:1}, {item:2}]  
}
I am getting the response now as -
Kevin Phillips
@phillipskevin
Jul 12 2016 18:05
can.Model.extend({
    parseModels: "data.items"
},{});
Sunil George
@georgesunil81
Jul 12 2016 18:05
{
  "isSuccessful": true,
  "message": "Successfully retrieved the items with the selected criteria!",
  "data": {
    "totalNumberOfItems": 57,
    "items": [{item:1}, {item:2}]
  }
}
Oh!
let me do that parsemodels
Hmmmm...that seems to have resolved the error...checking further...
Sunil George
@georgesunil81
Jul 12 2016 18:13
Yes @phillipskevin . That resolved the error for me. Thank you so much. Was clueless as to why this was happening. Thanks a ton. Keeps me moving.
Kevin Phillips
@phillipskevin
Jul 12 2016 18:13
good
Sunil George
@georgesunil81
Jul 12 2016 18:15
I have a question though @phillipskevin
How do I read the value of totalNumberOfItems?
My model does not return it, only returns the data.items array!
Any way I can use the same model to read the value of totalNumberOfItems?
Kevin Phillips
@phillipskevin
Jul 12 2016 18:20
you could overwrite parseModels as a function
Sunil George
@georgesunil81
Jul 12 2016 18:22
Okay...checking....
Kevin Phillips
@phillipskevin
Jul 12 2016 18:22
is totalNumberOfItems not just items.length?
Sunil George
@georgesunil81
Jul 12 2016 18:23
No, the totalNumberOfItems is the total found in the database.
items.length is the number of items returned in the current page.
Kevin Phillips
@phillipskevin
Jul 12 2016 18:24
ok
Sunil George
@georgesunil81
Jul 12 2016 18:24
Here is my model -
can.Model.extend({
    parseModels: "data.items",

    findAll: function(params, success, error) {

        return can.ajax({
                type: 'GET',
                url: '/item/All?',

            })
            .then(success, error);
    }

}
It is a findAll as you'd expect.
I put in the parseModel as you suggested and it resolved the error for me and I could get the array of items in the response. I iterate over the array in my stache and display each item.
But, I also want to read the value of "totalNumberOfItems"
I am trying to figure out how implementing parseModels as a function would provide me with both the "totalNumberOfItems" and the data.items array
Kevin Phillips
@phillipskevin
Jul 12 2016 18:36
you could try returning it as an object, I’m not sure
Sunil George
@georgesunil81
Jul 12 2016 18:39
Sure, let me try it...
Sunil George
@georgesunil81
Jul 12 2016 19:38
@phillipskevin On another topic, is there a preferred way to handle sessions with can.js applications? Any example I can look at?
I am having the need to store the API token and some user metadata info in session.
Matthew Phillips
@matthewp
Jul 12 2016 20:41
does sessions
Sunil George
@georgesunil81
Jul 12 2016 21:31
Oh, okay, I will check into it....thanks @matthewp
Thomas Sieverding
@Bajix
Jul 12 2016 22:05
@georgesunil81
function Session() {
  return can.ajax({
    url: config.hosts.api + '/session',
    type: 'GET',
    dataType: 'json',
    xhrFields: {
      withCredentials: true
    }
  }).promise();
}

session: {
  value: Session,
  type: Q,
  set: function( promise, setVal, onError ) {
    promise.then(setVal, onError);
  }
}