Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can/map/define default behaviors with "*" #1373

Closed
imjoshdean opened this issue Dec 16, 2014 · 14 comments · Fixed by #1618
Closed

can/map/define default behaviors with "*" #1373

imjoshdean opened this issue Dec 16, 2014 · 14 comments · Fixed by #1618
Milestone

Comments

@imjoshdean
Copy link
Contributor

It would be really nice if we could create default behaviors for map attributes. For example, I have an AppState object tied to my can.route; however, not every attribute I want to throw on the AppState should be serialized and put into the route. Right now I have to define every attribute I want to put in my AppState with:

attributeName: {
    serialize: false
}

What would be nicer to have would be to have a catch all definition for attribute behaviors, say, "*", that would provide a set of behaviors for all attributes unless otherwise defined. So for example:

serializeOne: {
    serialize: true
},
serializeTwo: {
    serialize: true
},
"*": {
    serialize: false,
    type: "string"
}

serializeOne and serializeTwo would be typed as strings and be serializable; all other attributes on the Map wouldn't be serialized but will be also typed as strings.

@mtovino
Copy link

mtovino commented Dec 16, 2014

+1

@zkat
Copy link
Contributor

zkat commented Dec 16, 2014

I like it. +1

@wclr
Copy link
Contributor

wclr commented Dec 21, 2014

This wil be probably usefull.

@imjoshdean
Copy link
Contributor Author

Word. I think I'll quick throw something together, add some tests, and make some documentation modifications. 👍

imjoshdean added a commit that referenced this issue Dec 28, 2014
…reaks with YUI, will figure out why later.
@imjoshdean
Copy link
Contributor Author

Currently sitting on a solution, but the tests are now failing in YUI - though I can't be exactly sure why. Will have to take a look later.

@daffl daffl added this to the 2.2.0 milestone Feb 4, 2015
@daffl daffl changed the title Proposal: can/map/define default behaviors with "*" can/map/define default behaviors with "*" Feb 9, 2015
@justinbmeyer
Copy link
Contributor

@daffl @imjoshdean what would it take to get this in for can.List as well?

@wclr
Copy link
Contributor

wclr commented Feb 14, 2015

Is * going to afffect ALL the attributes or only that are in define object?

@imjoshdean
Copy link
Contributor Author

@whitecolor It will affect all attributes - but it will have the same minimal footprint that define has already.

@wclr
Copy link
Contributor

wclr commented Apr 10, 2015

Is this supposed to work? In master?

@daffl
Copy link
Contributor

daffl commented Apr 10, 2015

Yes. But @imjoshdean still needs to add documentation (#1449)

@wclr
Copy link
Contributor

wclr commented Apr 10, 2015

http://jsfiddle.net/xKc3H/367/

var Model = can.Model.extend({

    define: {
        name: {
            value: 'Alex'            
        },
        '*': {
            serialize: false
        }
    }
})


var model = new Model({age: 10, name: 'John'})

console.log(model.serialize())

Why result is Object {name: "John"} ? I thought no props should be serialized, no? It is not very useful if * defines only not defnied attributes, I think it should aslo define each attribute properties.

@imjoshdean
Copy link
Contributor Author

@whitecolor Your use case is the entire reason why I wrote the define default behavior to begin with. I'll have a fix in before end of weekend.

imjoshdean added a commit that referenced this issue Apr 12, 2015
@imjoshdean
Copy link
Contributor Author

@whitecolor - Check the provided diff. I added your test with a minor tweak to the define tests. Thanks for catching this, because as I said...your use case was the whole purpose to me making this feature. I want to tie AppState to can.route, but I don't want EVERYTHING put in AppState to be serialized.

@imjoshdean
Copy link
Contributor Author

...aside from the linting erros. :S

daffl added a commit that referenced this issue Apr 14, 2015
Fix issue found in #1373 with not properly using defaults
imjoshdean added a commit that referenced this issue May 1, 2015
@imjoshdean imjoshdean removed their assignment Oct 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants