Canjs 3.2.1 can-connect get()

Hi,

I’m trying to change the todos MVC example to have a resource that returns an instance instead of a list:

Current code:

var AppViewModel = DefineMap.extend("AppViewModel",{
    appName: "string",
    todosList: {
        get: function(lastSet, resolve){
            Todo.getList({}).then(resolve);
        }
    }
});

Modified code:

var AppViewModel = DefineMap.extend("AppViewModel",{
    appName: "string",
    oneTodo: {
        get: function(resolve){
            Todo.get().then(resolve);
        }
    }
});

fixture("/api/todos", function() {
return { name: "mow lawn", complete: false, id: 5 };
});


proto-compute.js:357 Uncaught RangeError: Maximum call stack size exceeded
    at Compute.get (proto-compute.js:357)
    at Object.compute (can-compute.js:60)
    at AppViewModel.eval (can-define.js:512)
    at AppViewModel.get todos (index.js:58)
    at Compute._get (proto-compute.js:275)
    at Compute.get (proto-compute.js:379)
    at Object.compute (can-compute.js:60)
    at AppViewModel.eval (can-define.js:512)
    at AppViewModel.get todos (index.js:58)
    at Compute._get (proto-compute.js:275)

Does anyone knows how to do this ?

Thanks,
Nuno

Hi @nmasilva,

In your modified code, your get function only takes one argument, which makes it synchronous. When it has two arguments, it’s asynchronous, so you can set the value with whatever the promise returns.

The docs for asynchronous getters are on this page: https://canjs.com/doc/can-define/map/map.html#Declarativeproperties