1. #1
    Sencha - Services Team AndreaCammarata's Avatar
    Join Date
    Jun 2009
    Posts
    1,394
    Vote Rating
    22
    AndreaCammarata has a spectacular aura about AndreaCammarata has a spectacular aura about

      0  

    Question How to extend a Model

    How to extend a Model


    Hi guys,
    does anyone know if is possibile to extends an existing model using something like, for example

    Code:
    Ext.regModel('Product', {
           {name: 'id', type: 'int'},                  
           {name: 'name', type: 'string'}
    });
    
    Ext.ModelMgr.extends('Product', 'Book', {
           fields: [
                 {name: 'author', type: 'string'},                  
                 {name: 'pages', type: 'int'}
           ]
    });
    In this way I would have my Book model which inherit fields from "Product" ad implements some new fields. Is this already possibile to do?

    Thank you guys
    Sencha Inc
    Andrea Cammarata, Solutions Engineer
    CEO at SIMACS

    @AndreaCammarata
    www.andreacammarata.com
    github: https://github.com/AndreaCammarata

    TUX components bundle for Sencha Touch 2.x.x
    http://www.tux-components.com/


  2. #2
    jay@moduscreate.com's Avatar
    Join Date
    Mar 2007
    Location
    Frederick MD, NYC, DC
    Posts
    16,360
    Vote Rating
    81
    jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all

      0  

    Default


    There is nothing that I'm aware of in the framework that provides that functionality.

  3. #3
    Sencha - Services Team AndreaCammarata's Avatar
    Join Date
    Jun 2009
    Posts
    1,394
    Vote Rating
    22
    AndreaCammarata has a spectacular aura about AndreaCammarata has a spectacular aura about

      0  

    Default


    Quote Originally Posted by jgarcia@tdg-i.com View Post
    There is nothing that I'm aware of in the framework that provides that functionality.
    Thank you for your reply garcia.
    It would be really useful if Sencha develop this functionality.
    Every OOP language should be able to provide this feature, like we are already able to do for Sencha components.
    Sencha Inc
    Andrea Cammarata, Solutions Engineer
    CEO at SIMACS

    @AndreaCammarata
    www.andreacammarata.com
    github: https://github.com/AndreaCammarata

    TUX components bundle for Sencha Touch 2.x.x
    http://www.tux-components.com/


  4. #4
    jay@moduscreate.com's Avatar
    Join Date
    Mar 2007
    Location
    Frederick MD, NYC, DC
    Posts
    16,360
    Vote Rating
    81
    jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all

      0  

    Default


    Sencha is not an OOP Language, JavaScript is.

    That said, Models are not classes.

  5. #5
    jay@moduscreate.com's Avatar
    Join Date
    Mar 2007
    Location
    Frederick MD, NYC, DC
    Posts
    16,360
    Vote Rating
    81
    jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all

      0  

    Default


    FWIW, I think it would be good to add to the framework, though I've never had a need to do this.

  6. #6
    Sencha - Services Team AndreaCammarata's Avatar
    Join Date
    Jun 2009
    Posts
    1,394
    Vote Rating
    22
    AndreaCammarata has a spectacular aura about AndreaCammarata has a spectacular aura about

      0  

    Default


    Quote Originally Posted by jgarcia@tdg-i.com View Post
    Sencha is not an OOP Language, JavaScript is.

    That said, Models are not classes.
    Maybe I've not express myself in the right way.
    Yes, you are right javascript is OOP, but saying what you say do you mean that Sencha is not?
    Does not Sencha take the best from the language it is build on?
    I think yes, because we can use great features like

    Code:
    Ext.extends
    and

    Code:
    Ext.override
    So why not do the same with models?
    I'm working on some BIG Sencha Touch projects, and, sometimes, for example, I just want that the server gives me back the json data for a BASE model, because I don't want make a massive use of network for data I don't need.
    When I really need to take all the data for that object (example a Product), I want the server gives me back all the informations about it, so the COMPLETE model.
    I don't know if I was able to let you understand my needs.
    Sencha Inc
    Andrea Cammarata, Solutions Engineer
    CEO at SIMACS

    @AndreaCammarata
    www.andreacammarata.com
    github: https://github.com/AndreaCammarata

    TUX components bundle for Sencha Touch 2.x.x
    http://www.tux-components.com/


  7. #7
    Sencha Premium Member
    Join Date
    Sep 2008
    Posts
    710
    Vote Rating
    6
    Bucs is on a distinguished road

      0  

    Default


    So why not have overrides on the server methods that return limited sets of Product information, so you can call these when you only need limited data? The model is already on the client, so all you do is populated it with the limited data.

    Or, to organize it better, create object properties to hold the data that you may not need every time, and only populate it when needed. I am using MVC on the server and the client, and it would be trivial to add a property on the server model for say something like "Product.ExtendedInfo", which is of type ExtendedInfo that in turn is an object that has a lot more extraneous product detail info that may not be needed in every call. Then have your methods populate this object on the server depending on your needs. When it's brought to the client an serialized back into the model as an property of type 'auto', your ExtendedInfo property is either fully populated for use (like on a Product Detail page), or is null when not needed for say a ProductList page that only needs limited data.

    Not a true OO approach, but would more than suffice. Otherwise, you're probably going to have to try to play around with the prototype of the model I'm guessing, but that could get ugly, and not worth the effort imo.

  8. #8
    Sencha - Services Team AndreaCammarata's Avatar
    Join Date
    Jun 2009
    Posts
    1,394
    Vote Rating
    22
    AndreaCammarata has a spectacular aura about AndreaCammarata has a spectacular aura about

      0  

    Default


    Really thank you for your detailed reply Bucs.
    This could be a good compromise for my needs about Base and Complete models.
    But how you would handle the model for example "Book" that extends "Product"?
    I would really have a way to keep all the fields and even functions defined inside the "Product" model even inside the "Book" one.
    This would be really OO
    Sencha Inc
    Andrea Cammarata, Solutions Engineer
    CEO at SIMACS

    @AndreaCammarata
    www.andreacammarata.com
    github: https://github.com/AndreaCammarata

    TUX components bundle for Sencha Touch 2.x.x
    http://www.tux-components.com/


  9. #9
    Sencha Premium Member
    Join Date
    Sep 2008
    Posts
    710
    Vote Rating
    6
    Bucs is on a distinguished road

      0  

    Default


    You might want to try to create Sencha models on the client to isolate your required property sets into their own models, then use the hasMany (even though it's only a one to one relationship) and belongs to, to establish the required relationships. Then, if you implement overrides on the server data calls to return the required information sets, you will fill the appropriate models as needed.

    This would at least let you segregate model specific properties and functions into separate entities.

    So something like:
    Code:
    Ext.regModel("Product", {
        idProperty: "Id", //defaults to 'id'
        fields: [
            { name: "Id", type: "string" },
            { name: "Type", type: "string" },
            { name: "Desc", type: "string" },
            { name: "Price", type: "string" }
        ],
    
        hasMany: {model: 'Book', name: 'Books'}
    
    });
    
    Ext.regModel("Book", {
        idProperty: "BookId", //defaults to 'id'
        fields: [
            { name: "BookId", type: "string" },
            { name: "Genre", type: "string" },
            { name: "Author", type: "string" },
            { name: "Pages", type: "int" }
        ],
    
        belongsTo: 'Product'
    
    });
    Then is Product.Type = 'Book' you could do Book specific processing, or call Book specific functions. Again, I would have to say that Products in general tend to have enough common properties between them where this should not be necessary, but maybe that will help get you closer to what you're trying to do...until Sencha decides to allow model extension, or you figure out a clean way to use the model's prototype to accomplish this

  10. #10
    Sencha User
    Join Date
    May 2011
    Posts
    6
    Vote Rating
    0
    Iosis is on a distinguished road

      0  

    Default


    Still no suitable solution to this ?
    Its could be very usefull for big application.

Similar Threads

  1. Replies: 1
    Last Post: 16 Mar 2011, 10:40 PM
  2. Replies: 0
    Last Post: 17 Feb 2011, 5:21 PM
  3. Replies: 2
    Last Post: 17 Jun 2010, 6:53 AM
  4. Ext.ux.extend - extension of Ext.extend )
    By SamuraiJack1 in forum Ext 2.x: User Extensions and Plugins
    Replies: 5
    Last Post: 7 Oct 2008, 10:08 AM

Thread Participants: 7