I am creating an application that allows users to post to Twitter. When they click the submit button, we close the posting form. We create a Message object that is stored in the data warehouse and sent to the server. The server creates a Post object, then sends the request to Twitter. Then the server updates the Post object, returns the updated information back to the user interface.
This part is already working. But I need to know if it does not work, so that I can warn the user that their message has not passed, and keep an open publication form. Here are some localized details about my application.
Social.Message = DS.Model.extend({ text: DS.attr("string"), created: DS.attr("date"), isPending: DS.attr("boolean"), posts: DS.hasMany("Social.Post") }); Social.Post = DS.Model.extend({ text: DS.attr("string"), status: DS.attr("string"), created: DS.attr("date"), message: DS.belongsTo("Social.Message"), msgStatus: function() { return ((this.get('status') === 'S') ? true : false); }.property('status') });
The life cycle of a message (status) goes from P (waiting) to Q (in line) to S (sent), E (error) is also the opportunity and status that I'm really looking for. Here's the saveMessage method:
saveMessage: function(text){ var acct = Social.Account.find(this.get("id")), msg = Social.store.createRecord( Social.Message, { text: text, created: new Date() } ); acct.get("messages").addObject(msg); Social.store.commit(); Ember.run.later(this, function() { msg.get('posts').forEach(function(p){ p.reload(); }); }, 1000); }
You can see that I pause for a second to make the server work, and then try to reload the Post object with the response from Twitter. Those last few lines where I think this new code will go, but I'm not sure how to listen to something that might not come back. I would rather not "wait" a second, instead it would be nice if the message could just be updated. Not sure how to do this.
Thoughts?