skariel / webalchemy

Modern web development with Python
MIT License
346 stars 21 forks source link

How to push/pull with webalchemy #111

Closed sthzg closed 10 years ago

sthzg commented 10 years ago

Based on the todomvc example, what is the best way of pushing results to the server? If I am getting it right, the data model in this example lives on the client. I guess if I want to communicate with the server, I could fetch an event (or bind a separate button in the UI) and use the existing websocket to push the data? Is there an example that demonstrates how this is done with the framework?

skariel commented 10 years ago

Yes the TodoMVC data model lives on the client. The simplest way to communicate to the server is to use the message(str) function, for e.g. in the datamodel:

def remove_completed(self):
    self.itemlist = self.itemlist.filter(lambda i:  not i.completed)
    self.persist()
    message('completed removed')

and then in your app add the incoming message method inmessage(str) to handle this for e.g.:

def inmessage(self, msg):
    log.info(msg)

Please note: I just fixed this mechanism so use the latest code

Now on top of this there's an RPC layer (i.e. call any server function that was registered for this) from the client, it is demonstrated in the meteor_colors_app_example but I'm currently rewriting this code. When that is done, I'll implement a higher level interface: data binding between client and server.

Please let me know if the current solution fit your needs or not this is important feedback :)

sthzg commented 10 years ago

Thanks for your reply. I'll update the sources, play around with the code and give you feedback as soon as I have it. :)

skariel commented 10 years ago

some news:

1)

RPC is now refactored you can do stuff like the following:

define in your app some function to be called from the client:

def callme(self, callerid, msg):
    log.info('I was called, it works!'+msg)

then call it from the client, say from the datamodel:

def remove_completed(self):
    self.itemlist = self.itemlist.filter(lambda i:  not i.completed)
    self.persist()
    rpc(self.callme, 'completed removed!')

and 2)

if you want to push data you can do now in your app stuff like:

self.datamodel.itemslist[0].text = 'hi there from the server'

Note that all this functionality was in place before but just translated like this to Python. I'm closing this issue, I'll open a new one to figure out automatic data binding between server and client and another one to translate the colors app to pure Python