These are chat archives for canjs/canjs

3rd
May 2016
DrJochum
@DrJochum
May 03 2016 21:00

In my projekt I have a Map with different countries that have different products and different languages. We also have a map with the translations.
I would like to do something like this in the template:
{{vLang[vCountry]}}
I also tried with helpers but could not succeed.

Here is a simplified idea. I hope it explains the problem:

import oneTemplate from 'onetemplate.stache!';

var lang = new can.Map({
    "en": {
        "book": "book",
        "tree": "tree",
        "green": "green"
    },
    "de": {
        "book": "Buch",
        "tree": "Baum",
        "green": "grün"
    }
});

var country = can.compute("en");

$('body').html( oneTemplate({
    vLang: lang,
    vCountry: country
}) );

//stache template onetemplate.stache

{{vLang[vCountry]}}
dylanrtt
@dylanrtt
May 03 2016 21:02
@DrJochum that's not currently possible because there is no index operator, so you need a helper. Let me find an example
DrJochum
@DrJochum
May 03 2016 21:36
Great! I tried something similar that did not work...
You pushed me in the right direction!
Guido Smeets
@gsmeets
May 03 2016 22:02
is there an example of binding to a list promise getter?
Sunil George
@georgesunil81
May 03 2016 22:03
Hello, I would appreciate if anyone has a solution or has come across this problem - I have to make API calls from my application (which is using canjs). The APIs are on a different domain than where my front-end application is. So, how do I prefix the domain to every API ajax calls and how do I handle fixtures in this scenario?
I want to prefix the API domain (for example "http://apiserver.api.com") to every API ajax call.
Guido Smeets
@gsmeets
May 03 2016 22:04
depends a bit on the rest of your stack
I use requirejs, and prefix all my model rest methods with it:
findAll: require.toUrl( "~/api/agenda/Afspraak" )
dylanrtt
@dylanrtt
May 03 2016 22:05
@gsmeets Do you mean a getter that returns a promise that resolves to a list or are you talking about using the list/promise plugin?
Sunil George
@georgesunil81
May 03 2016 22:05
I am using StealJS and using AMD format
Guido Smeets
@gsmeets
May 03 2016 22:06
I reckon steal has some url mapping function as well
but I'm unfamiliar with it
Kevin Phillips
@phillipskevin
May 03 2016 22:06
if you want to do it for all environments you can use: http://api.jquery.com/jquery.ajaxprefilter/
Guido Smeets
@gsmeets
May 03 2016 22:06
@dylanrtt I think I have that plugin enabled yes
Kevin Phillips
@phillipskevin
May 03 2016 22:07
if you’re just looking for a way to modify the url of every ajax request
this is also aliased to can.ajaxPrefilter
Guido Smeets
@gsmeets
May 03 2016 22:09

I'm trying to simplify some code to a getter that returns a promise:
Viewmodel:

foo: {
    get: function () {
        return Foo.findAll();
    }
}

Stache:

{{#if foo.isPending}}
{{#else}}
    ...?
{{/}}
dylanrtt
@dylanrtt
May 03 2016 22:10
@gsmeets you could use 2 properties, one for the promise and the other for the value. I have seen that pattern emerging lately
Guido Smeets
@gsmeets
May 03 2016 22:11
heh, that's precisely what I was trying right now
dylanrtt
@dylanrtt
May 03 2016 22:11
otherwise, the syntax for the list/promise plugin is to return new MyModel.List({/*params*/}) which is a promise-like list
Guido Smeets
@gsmeets
May 03 2016 22:11
was adding a done(function() { this.attr("loaded") }
ah right, that's the syntax indeed
I've used that in the past some time
dylanrtt
@dylanrtt
May 03 2016 22:12
I would be careful with that plugin, though. I recently had an issue where the existence of that plugin causes all can.Lists to be promise-like, so if you return one in a promise, it crashes the app
Guido Smeets
@gsmeets
May 03 2016 22:13
yeah, I do recall it having some downsides
dylanrtt
@dylanrtt
May 03 2016 22:14
personally, I don't think it adds much value and would recommend just using 2 properties which is clearer
I am having to pull it out of my current project today because of the problems it causes
Guido Smeets
@gsmeets
May 03 2016 22:14
there's just something in me that feels it's dirty as hell to add a side effect to a getter like that
dylanrtt
@dylanrtt
May 03 2016 22:14
the getter doesn't need to have a side effect
Guido Smeets
@gsmeets
May 03 2016 22:15
ah, that's neat
is that 2.2 compatible?
dylanrtt
@dylanrtt
May 03 2016 22:16
should be
Guido Smeets
@gsmeets
May 03 2016 22:20
aye, that works
that's a pretty neat solution
Sunil George
@georgesunil81
May 03 2016 22:37
Thanks @phillipskevin . I will look at can.ajaxprefilter
@phillipskevin . I tried $.ajaxprefilter but it was not getting called eventhough ajax requests were being made! I just tried can.ajaxprefilter and it does get called on the ajax requests.
Kevin Phillips
@phillipskevin
May 03 2016 22:41
ok
Sunil George
@georgesunil81
May 03 2016 22:41
Any particular reason why we are not being able to call $.ajaxprefilter
Will use can.ajaxprefilter, but was just curious as to why $.ajaxprefilter does not work...
Kevin Phillips
@phillipskevin
May 03 2016 22:45
I’m not sure
Guido Smeets
@gsmeets
May 03 2016 22:53
My guess is canjs overrides yours
Kevin Phillips
@phillipskevin
May 03 2016 22:54
that’s probably right
or maybe there are 2 versions of jquery on the page