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

Allow to assign object as attribute type (in can.Observe) #107

Closed
wclr opened this issue Sep 17, 2012 · 11 comments
Closed

Allow to assign object as attribute type (in can.Observe) #107

wclr opened this issue Sep 17, 2012 · 11 comments

Comments

@wclr
Copy link
Contributor

wclr commented Sep 17, 2012

Currently one can assign model/list name (string) as attribute type for automatic conversion. But if there is no such object in window context (for an example AMD case) it is impossible to do so.

I would propose to use model/list objects it self for declaring attribute type.

steal('can/observe/attributes', 'items.js', function(Observe, Items){
    return Observe({
    attributes: {           
        items: Items // Items is object not 'Items' string
    }
})

then observe knows what type to convert to.

@justinbmeyer
Copy link
Contributor

How does it know to call new Items or simply Items? I don't know if this will work.

@justinbmeyer
Copy link
Contributor

You can pass attributes functions. That will work.

@wclr
Copy link
Contributor Author

wclr commented Sep 17, 2012

why call just Items, in what case does it make sense?

at list if Items is Construct/Observer or Observe.List/Model.List it does make sense to all new Items, no?

@justinbmeyer
Copy link
Contributor

attributes:{
items: function(raw){ return new Items(raw) }
}

Sent from my iPhone

On Sep 17, 2012, at 10:29 AM, Alex Osh notifications@github.com wrote:

Currently one can assign model/list name (string) as attribute type for automatic conversion. But if there is no such object in window context (for an example AMD case) it is impossible to do so.

I would propose to use model/list objects it self for declaring attribute type.

steal('can/observe/attributes', 'items.js', function(Observe, Items){

return Observer({
attributes: {

    items: Items // Items is object not 'Items' string

}
})

then observe knows what type to convert to.


Reply to this email directly or view it on GitHub.

@wclr
Copy link
Contributor Author

wclr commented Sep 17, 2012

Yes, using function is good, but why not make it automatically.

@justinbmeyer
Copy link
Contributor

There's no great way to tell a function that is intended to be called with new vs a normal function.

In js a constructor function is just a function. It's how you call it that matters.

Sent from my iPhone

On Sep 17, 2012, at 11:13 AM, Alex Osh notifications@github.com wrote:

why call just Items, in what case does it make sense?

at list if Items is Construct/Observer or Observe.List/Model.List it does make sense to all new Items, no?


Reply to this email directly or view it on GitHub.

@justinbmeyer
Copy link
Contributor

Because it's very common to not pass a constructor and a function that might pick between various constructors. Or pass something like Date.parse.

Sent from my iPhone

On Sep 17, 2012, at 11:19 AM, Alex Osh notifications@github.com wrote:

Yes, using function is good, but why not make it automatically.


Reply to this email directly or view it on GitHub.

@wclr
Copy link
Contributor Author

wclr commented Sep 17, 2012

Ok, it's just proposal. You know better. But for standard objects like Costruct/Observer/Model/List probably it could be done.

@daffl
Copy link
Contributor

daffl commented Sep 17, 2012

I was more thinking along the line of the object providing its own conversion and serialization methods so that you don't have to implement an additional wrapper:

return can.Construct({
    convert : function() {

    },
    serialize : function() {

    }
}, {});

steal('can/observe/attributes', 'items.js', function(Observe, Items){
    return Observe({
    attributes: {           
        items: Items // Items is object not 'Items' string
    }
});

I think #106 is related to that as well (automatic conversion in Observe.List subclasses).

@wclr
Copy link
Contributor Author

wclr commented Sep 18, 2012

Yes they (107 and 106) relates. What I propose that if you wnat to get Observe.List named for example Blocks = Block.List (which assumed to be collection of elements of type Block), instead of code:

attributes: {
            items: function(items){
                return new Blocks(items.map(function(item){
                    return new Block(item)
                }))
            }
        }

It would be much more elegant to write just like that:

attributes: {
            items: Blocks
        }

@daffl
Copy link
Contributor

daffl commented Oct 16, 2012

Hm, GitHub wasn't supposed to close the issue from a branch.

Anyway, @whitecolor check out this branch. The attributes plugin lets you do things like this now:

var Sword = can.Observe({
    getPower : function() {
        return this.attr('power') * 100;
    }
});

var Level = can.Observe({
    getName : function() {
        return 'Level: ' + this.attr('name');
    }
});

var Zelda = can.Observe({
    attributes : {
        sword : Sword,
        levelsCompleted : Level.List
    }
},{});

var link = new Zelda({
    sword : {
        name : 'Wooden Sword',
        power : 0.2
    },
    levelsCompleted : [
        {id: 1, name: 'Aquamentus'},
        {id: 2, name: 'Dodongo'}
    ]
});

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

No branches or pull requests

3 participants