These are chat archives for canjs/canjs

9th
Aug 2017
Viktor Busko
@Lighttree
Aug 09 2017 13:44

Hi guys, I have one question regarding can-connect. I have following model:

import DefineMap from 'can-define/map/map';
import DefineList from 'can-define/list/list';
import set from 'can-set';
import connect from 'can-connect';
import parse from 'can-connect/data/parse/parse';
import constructor from 'can-connect/constructor/constructor';
import canMap from 'can-connect/can/map/map';
import fallThroughCache from 'can-connect/fall-through-cache/fall-through-cache';
import constructorStore from 'can-connect/constructor/store/store';
import memoryCache from 'can-connect/data/localstorage-cache/localstorage-cache';
import realTime from 'can-connect/real-time/real-time';
import authBehavior from './authBehavior';

// Connection behaviors list
const behaviors = [realTime, authBehavior, fallThroughCache, constructorStore, parse, constructor, canMap];

const cacheConnection = connect([
    memoryCache
], {
    name: 'entity'
});

const Entity= DefineMap.extend({
    id: 'number',
    creatorId: 'number',
    message: 'string',
    title: 'string',
});

const algebra = new set.Algebra(
    set.props.id('id')
);

Entity.List = DefineList.extend({
    '#': Entity
});

Entity.connection = connect(
    behaviors, {
        url: '/api/v1/entity',
        Map: Entity,
        List: Entity.List,
        name: 'entity',
        parseInstanceData(responseData) {
            return responseData.checkinVO;
        },
        algebra,
        cacheConnection
    });

export default Entity;

I have list of entities on the page:

export const ViewModel = DefineMap.extend({
    entityListPromise: {
        get() {
            return Entity.getList({});
        }
    },
    entityList: {
        Type: [Entity],
        get(lastSet, resolve) {
            this.entityListPromise.then(resolve);
        }
    }
});

I need to add new item to sever and expecting that this list will be updated automatically by:

this.entity.save()

but it seems that it doesn't work :( I can see new item in the list only after page refresh.

I've tried this as well:

this.entity.save().then((response) => {
      Entity.connection.createInstance(response);
});

But didn't succeed as well.
Is it expected that can.js will update list automatically or I have to push this item into list by myself ?

kostya.aderiho
@kaderiho
Aug 09 2017 13:49

Hi everyone, could someone help with the next question:

I'm using can.connection with these behaviors: url, constructor, canMap and getting the list of instances using getList():

[{priorityId: 1, description: 'some text'}, {priorityId: 2, description}, ....] .

When I call .save() as a POST request it works fine, but when I try to UPDATE information of this instance my URL is wrong and becomes just like localhost:3000/. As I understood canjs expects to see ID in the list of instance props instead of priorityId.

How I can parse the response of getList() and replace priorityId field on id? Does can.Algebra help with it? Thanks in advance!

kostya.aderiho
@kaderiho
Aug 09 2017 14:41

BTW, when I use this algebra:

const algebra = new set.Algebra( set.props.id('priorityId') );

My .get() request looks wrong: localhost:3000/?id=283, but without Algebra it looked fine: https://localhost:3000/priorities/283

Will Stanton
@wstant
Aug 09 2017 19:54
Hey everybody, is it possible to test two DefineMaps for equivalency using chai? deep.equal() doesn't seem to be working for me
Kevin Phillips
@phillipskevin
Aug 09 2017 19:55
I would .serialize() them first
Will Stanton
@wstant
Aug 09 2017 19:55
Hey Kevin!
Thanks, I'll try that out
Will Stanton
@wstant
Aug 09 2017 20:02
Still having trouble, any other suggestions?
Kevin Phillips
@phillipskevin
Aug 09 2017 20:09
what is the issue?
Will Stanton
@wstant
Aug 09 2017 20:18
I'm trying to test a function which updates two properties in route.data- page and flow. The function updates the properties correctly, and if I test the equivalency of each property to the desired value, the test passes
When I try to test the function this way route.data.should.deep.equal(routeTestObject);
I get this assertion error: AssertionError: expected { Object (page, flow) } to deeply equal { Object (page, flow) }
let me know if you'd like to see the code for the test or the function, they're both quite small
Kevin Phillips
@phillipskevin
Aug 09 2017 20:23
and you tried route.data.serialize().should.deep.equal(routeTestObject.serialize());?
Will Stanton
@wstant
Aug 09 2017 20:26
ah I see, I tried route.data.serialize(); and routeTestObject.serialize(); before the test. It works with .serialize() in the test line
thanks Kevin
Kevin Phillips
@phillipskevin
Aug 09 2017 20:27
cool, no problem, Will