These are chat archives for canjs/canjs

7th
Jun 2016
Matthew Phillips
@matthewp
Jun 07 2016 00:04
You can use a define plugin type to convert it to an array.
type: function(val) { return val.split(","); }
Sunil George
@georgesunil81
Jun 07 2016 14:16
@phillipskevin - I think I have a clue why my define getters are not getting called. The stache page I have is built up by two different models!
Kevin Phillips
@phillipskevin
Jun 07 2016 14:18
what do you mean?
Sunil George
@georgesunil81
Jun 07 2016 14:18
Both the models, though getting data from different sources have the same-named getter functions.
Kevin Phillips
@phillipskevin
Jun 07 2016 14:19
ok
Sunil George
@georgesunil81
Jun 07 2016 14:19
So, I guess that is leading to the ambiguity since I am not specifying anywhere in my stache which one should get called!
So, should I name the getter functions unique or there is a way to resolve the ambiguity since I believe a page can have multiple data models!
Kevin Phillips
@phillipskevin
Jun 07 2016 14:20
your template can only be rendered with one scope
Sunil George
@georgesunil81
Jun 07 2016 14:21
Correct. I am merging data from two models into one scope.
Kevin Phillips
@phillipskevin
Jun 07 2016 14:21
how are you doing that?
Sunil George
@georgesunil81
Jun 07 2016 14:21
And then passing the scope to the stache template.
Kevin Phillips
@phillipskevin
Jun 07 2016 14:22
can you show the code where you’re merging the models?
Sunil George
@georgesunil81
Jun 07 2016 14:23
In the .done of the models, I am appending up the response from the model into the scope.
I guess, it may not be the best practice approach!
Kevin Phillips
@phillipskevin
Jun 07 2016 14:24
if you’re just taking the response… that won’t have your virtual properties
you need to store the instance of the model in your scope
Sunil George
@georgesunil81
Jun 07 2016 14:25
Hmmm, could you elaborate? I may be missing something here. I believe the getter functions are what you mean by virtual properties.
Kevin Phillips
@phillipskevin
Jun 07 2016 14:25
right
Sunil George
@georgesunil81
Jun 07 2016 14:25
Ah, how do I store the instance of the model in my scope!? I think that is what I am missing.
Could you give me a quick example/code snippet?
Kevin Phillips
@phillipskevin
Jun 07 2016 14:26
yeah, let me find one real quick
Sunil George
@georgesunil81
Jun 07 2016 14:27
Thank you so much. I think that is what I a missing, not putting the instance of the model on scope!
Sunil George
@georgesunil81
Jun 07 2016 14:27
Thanks @phillipskevin. Will take a look into it.
Kevin Phillips
@phillipskevin
Jun 07 2016 14:28
so in that example, restaurants in the scope of that component is the promise returned by the model
so where they use restaurants.value in the template, that’s the instance of the model
calling that set is asynchonously setting the value of game to the instance of the model returned by gamePromise
so then game can be used in the template to access any properties from the model, including values using getters
Sunil George
@georgesunil81
Jun 07 2016 14:31
Makes sense to me.
I missed it.
Kevin Phillips
@phillipskevin
Jun 07 2016 14:32
so instead of merging your two models’ data, store them separately on your viewModel
and then you should be able to use the getter values from either one
Sunil George
@georgesunil81
Jun 07 2016 14:44
Hmmm, thats a good suggestion. Thanks @phillipskevin. Will do.
Ilya Fadeev
@ilyavf
Jun 07 2016 17:11

hi! a question about can-connect. How can I create a "base model" to customize requests so that particular headers would be sent with each call?

I can customize every call separately for data-url strategy (e.g. getListData, etc), but I need to do it for all my models&methods generically:

export const traceConnection = superMap({
  url: {
    resource: '/api/traces',
    getListData: function(params){
      return $.get({
        url: '/api/traces',
        headers: {
          'X-Tracebase-Token': params.token
        }
      });
    }
  }, ...
Justin Meyer
@justinbmeyer
Jun 07 2016 17:11
@ilyavf you can create your own behavior
I'm not sure if @DesignByOnyx made superMap able to accept other behaviors
if not, you can either make it accept other behaviors
or, you can manually combine behaviors into your own connection
Ilya Fadeev
@ilyavf
Jun 07 2016 17:13
@justinbmeyer: thanks, a custom behaviour is what i thought to try
Justin Meyer
@justinbmeyer
Jun 07 2016 17:15
alternatively, you could just make an options mixin
makeGetListData = function(url){
  return  function(params){
      return $.get({
        url: url,
        headers: {
          'X-Tracebase-Token': params.token
        }
      });
    }
}
export const traceConnection = superMap({
  url: {
    resource: '/api/traces',
    getListData: makeGetListData('/api/traces')
  }, ...
Ilya Fadeev
@ilyavf
Jun 07 2016 17:16
but this is too verbose
i will have to add this mixin into every CRUD operation of every model (
Justin Meyer
@justinbmeyer
Jun 07 2016 17:17
you could make it generate the whole url part
export const traceConnection = superMap({
  url: makeUrlConfig('/api/traces'), ...
Ilya Fadeev
@ilyavf
Jun 07 2016 17:21

hm... i like this.

or I can just customize a lower level transport to add the necessary headers (jquery.ajax). Would this be a good practice?

Ryan Wheale
@DesignByOnyx
Jun 07 2016 17:22
We needed a custom behavior recently - and I was tasked with making superMap accept other behaviors. The main reason I haven't implemented it yet because our situation required that the behavior be added in the middle, and there's currently no elegant way to do that.
Ilya Fadeev
@ilyavf
Jun 07 2016 17:24
@DesignByOnyx: why is that? i though you just have to coma-separate the behaviours (so to add one into the middle should be simple)?
Ryan Wheale
@DesignByOnyx
Jun 07 2016 17:26
Should users pass in all the behaviors they want to supermap when all they want is to add a custom one?
I mean, all supermap does is create a connect object with the kitchen sink of behaviors - I don't think users should really have to know that list of behaviors. But there needs to be some sort of API like superMap(options).addBehaviorBefore('localstorage-cache', behavior); or something - I don't know what that should look like thought @justinbmeyer.
Justin Meyer
@justinbmeyer
Jun 07 2016 17:30
@DesignByOnyx I don't think they should
somehow, they should be able to pass in a diff
the ones to add and to remove
yeah, the order problem is .... well a problem
I don't think knowing what behaviors are in supermap is a problem
Ryan Wheale
@DesignByOnyx
Jun 07 2016 17:32
I have to jump on a call real quick. I
Justin Meyer
@justinbmeyer
Jun 07 2016 17:32
they are listed as part of the docs
Ryan Wheale
@DesignByOnyx
Jun 07 2016 17:32
I'd like to implement this though... be back soon
Justin Meyer
@justinbmeyer
Jun 07 2016 17:32
superMap(add, remove, options)
something like that
but yeah, order is the problem
Ryan Wheale
@DesignByOnyx
Jun 07 2016 17:59
Are you opposed to a more "conversational" API? superMap(options).addBehaviorBefore('localstorage-chache', customBehavior).removeBehavior('combine-requests').replaceBehavior('inline-cache', customBehavior);
This would satisfy all of the use cases I've encountered over the past 6 months where I've needed to add/remove/and customize behaviors.
Ilya Fadeev
@ilyavf
Jun 07 2016 18:12
a dummy question: is a list of behaviours a pipeline or just a list of mixin objects?
Mason Shin
@minsooshin
Jun 07 2016 18:38
canjs website is quite slow. does this happen only me? someone can confirm?
Paul Tichonczuk
@tracer99
Jun 07 2016 18:41
@daffl you menioned once that there was a mustache to stach conversion script you guys had
do you have that handy?
Ryan Wheale
@DesignByOnyx
Jun 07 2016 19:10
@ilyavf - It's a prototype hierarchy. Each behavior becomes the prototype for the next. When a behavior calls a method on the "base" behavior, it's referring to the underlying prototype for that behavior ("super" of sorts - but not classes)
So you might have a connect object made up of Behavior A and Behavior B. Behavior A becomes the prototype of Behavior B. If both implement a getListData method, both should eventually call the "base" getListData. When Behavior B calls base.getListData - it's calling Behavior A's implementation. When Behavior A calls base.getListData it's calling the core connect method. Does that make sense?
Matthew Phillips
@matthewp
Jun 07 2016 19:20
Justin Meyer
@justinbmeyer
Jun 07 2016 19:26
@minsooshin loaded quick for me
is there a particular page?
Mason Shin
@minsooshin
Jun 07 2016 19:26
now it loads quick :)
thanks
is there any viewmodel reference for canjs? I would like to add that to wikipedia https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel
it only has knockout.js
:)
Ilya Fadeev
@ilyavf
Jun 07 2016 19:37
@DesignByOnyx: yes, this makes sense, thank you!
Paul Tichonczuk
@tracer99
Jun 07 2016 19:55
@matthewp thanks
Mason Shin
@minsooshin
Jun 07 2016 21:40
how I can set can.ajaxSettings not to use JSON.stringify for data?
I want to keep boolean data type, but without JSON.stringify it turns to string value.
Dovid Bleier
@dbleier
Jun 07 2016 21:46
when deleting an instance via can-connect.destroy, what is the content and format of the data expected back from the server?
Ryan Wheale
@DesignByOnyx
Jun 07 2016 22:47
@minsooshin - can you clarify a bit? Boolean is one of the datatypes used by JSON - so if you stringify a boolean, it should not have quotes around it. JSON.stringify({foo: true}) //-> "{"foo":true}"
Whenever that is parsed, it should be a primitive Boolean, not a string Boolean
Mason Shin
@minsooshin
Jun 07 2016 22:48
sorry, to clarify, I am using default can.Model resource ajax call, and it uses formdata for my passing param
so, boolean value translated to string, on the http request
ajax default content-type is 'application/x-www-form-urlencoded; charset=UTF-8’
Ryan Wheale
@DesignByOnyx
Jun 07 2016 22:50
ah, yeah, thats b/c of form-urlencoded
Mason Shin
@minsooshin
Jun 07 2016 22:50
yes
Ryan Wheale
@DesignByOnyx
Jun 07 2016 22:50
you need something on the server to convert things to their primitive values
Mason Shin
@minsooshin
Jun 07 2016 22:51
so I would like to know setup the ajax envronment for can.Model
but, I figured out to solve this
Ryan Wheale
@DesignByOnyx
Jun 07 2016 22:51
I just did this on my current project
are you using express?
Mason Shin
@minsooshin
Jun 07 2016 22:51
since I don’t want to affect on other can.model ajax calling, I will just do type casting in the rails API
Ryan Wheale
@DesignByOnyx
Jun 07 2016 22:52
yeah, it has to be done on the server
form-urlencoded makes everything a string
Mason Shin
@minsooshin
Jun 07 2016 22:53
yes
thanks @DesignByOnyx :)