[MacRuby-devel] Manage array of objects within object's class?
Jeff Dyck
fsjjeff at gmail.com
Fri May 17 11:52:14 PDT 2013
Hey,
I'm working on developing an app in MacRuby that uses HTTParty to consume a Web Service. I'm hoping someone can help me wrap my head around some of the Model - View - Controller stuff...
From reading some tutorials on MacRuby (and RubyMotion), it seems that it's best practice to have all the HTTP requests for loading, saving and querying objects as part of the model, rather than in the controller (which is what I've done in the past)...
I can wrap my head around saving and deleting objects in the Model, but wondering about loading objects - in particular when there are many objects to be loaded... If my code to load the JSON is embedded in the Model as a Class Method, can I also have a Class Array to store them? And if so can I bind that to an NSArray Controller and use that to feed my UI?
IE: I have a class of 'sites' which is basically a list of schools in our District, and simplified looks something like:
class Site
require 'httparty'
include HTTParty
format :json
base_uri 'http://webservice.address.here'
attr_accessor :siteArray
attr_accessor :siteArrayController
PROPERTIES = [:id, :code, :name, :server, :location, :saved]
PROPERTIES.each { |prop| attr_accessor prop }
def self.getAllSites
siteArray = NSMutableArray.new
get('/sites').parsed_response.each do |site|
siteArrayController.addObject(self.new(site))
end
end
def initialize(attributes = {})
attributes.each { |key, value|
self.send((key.to_s + "=").to_s, value)
}
if @id.nil? then
@saved = false
else
@saved = true
end
end
end
Basically, I would like to call from the main App Delegate something like Site.getAllSites, which would talk to the Web Server, load and parse the json and init the actual objects, adding them to the siteArray, which would in turn populate the UI via Bindings.
So I guess my questions are:
1) Am I crazy trying to do it like this?
2) How do I define the siteArray and siteArrayController so that they are Class variables (rather than object variables) and can be bound to the UI?
Thanks in advance for anyone who can point me in the right direction.
Jeff
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20130517/6b2f9414/attachment.html>
More information about the MacRuby-devel
mailing list