Using SPOD Object in Flash

115 views
Skip to first unread message

Jimmy Delas

unread,
Nov 4, 2012, 7:34:07 PM11/4/12
to haxe...@googlegroups.com
Hi,

I use haxe.remoting to deal with my database. On my server are stored SPOD Object. But since SPOD is not available in flash, i cannot retrieve them and I have to duplicate all my SPOD Objects in a comprehensive form for Flash. Maybe I'm not dealing with the database correctly so i would ask to you how do you deal with database from Flash through PHP server for example ?

For a better sample, I have a Server class for the remoting in PHP, a client class for the remoting in Flash. Then, I have a User class which extends from SPOD object, but since it's not available in Flash like all SPOD types, I have to duplicate the User class in Flash and I think it's a really bad solution :/

Regard,
Jimmy

Jason O'Neil

unread,
Nov 4, 2012, 9:00:19 PM11/4/12
to haxe...@googlegroups.com
The short answer is to use Conditional Compilation, but there are a few gotchas...

#if php
    import sys.db.Types;
#else

    import SpodRemoteTypes;
#end

class User #if php extends sys.db.Object #end
{
    #if !php public function new() {} #end

    public var id : SId;
    public var name : SString<32>;
    public var birthday : SDate;
    public var phoneNumber : SNull<SText>;

  
    #if php
        public static var manager = new sys.db.Manager<User>(User)
    #end
}


The basic notes:
  • Use conditional compilation so only your server targets (PHP/Neko) extend sys.db.Object
  • Your client targets will need a constructor, because they're not inheriting a class with one anymore
  • You don't have to worry about metadata from the SPOD class, but the custom types (eg. SId or SString) won't compile at first, because they are defined in sys.db.Types, and you can't access the 'sys' package from a client target.  The solution is basically to copy and paste the sys.db.Types file into your own haxe file in a package the client can access.  I've attached a copy of the file I used here.  If you import this, both targets should compile fine.
Have a go and see if something like that works for you, I know I've gotten it to work before, with remoting working between the targets so objects can be transferred seamlessly.  Hopefully it won't be too hard :)

Jason


SpodRemoteTypes.hx

Jason O'Neil

unread,
Nov 4, 2012, 10:50:15 PM11/4/12
to haxe...@googlegroups.com
An alternative I just thought of, which might be cleaner, is to have a custom class "my.db.Object", which you always extend.

package my.db;

#if (php || neko)

    typedef Object = sys.db.Object;

#else

    class Object
    {
        public function new() {}
    }

#end

This way, your code doesn't have to get ugly with conditional compilation all over the place.  Just extend this object, and it'll work on your server, and it will also work on your client.  If you're feeling really clever, you could even add some helper methods to your client-side object, for example "save()" could start a remoting call that calls save() on the server.

Might be worth a blog post exploring these tricks :)

Hope this helps,
Jason

tom rhodes

unread,
Nov 5, 2012, 3:40:16 AM11/5/12
to haxe...@googlegroups.com
nice, never thought abotu this, i am mapping the SPOD objects to client side only objects. i'll definitely have a go at the universal object idea! 

would be nice if SPOD did it out of the box though ;)

Jimmy Delas

unread,
Nov 5, 2012, 5:04:32 AM11/5/12
to haxe...@googlegroups.com
Nice idea. It could be the subject for a new library or just an extension of SPOD for Flash support :)

I think that a more generic communication system is needed here. But with this tricks, you can save lot of code when you create a new SPOD object which need to be shared to Flash.
Reply all
Reply to author
Forward
0 new messages