Manage array of objects within object's class?
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
Hi Jeff, I'm no guru but I wouldn't put the methods for dealing with the collection into the model class. You could indeed have a class array (by using the @@prefix for your variable name) but you could not easily access it in a KVC compliant manner and therefore could not directly bind it to an NSArrayController. Steve On 17 May 2013, at 19:52, Jeff Dyck <fsjjeff@gmail.com> wrote:
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 _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org https://lists.macosforge.org/mailman/listinfo/macruby-devel
Fair enough Steve, thanks for the reply - I'm still working on figuring out this MVC thing and keeping the logic of my app separate. I suppose I could have the code to load the JSON into a hash within the model, but keep the actual object creation and collection outside. At least then all the methods for talking to the server would be in one place, embedded into the model. Jeff On May 18, 2013, at 12:04 AM, Steve Clarke <steve@sclarkes.me.uk> wrote:
Hi Jeff,
I'm no guru but I wouldn't put the methods for dealing with the collection into the model class.
You could indeed have a class array (by using the @@prefix for your variable name) but you could not easily access it in a KVC compliant manner and therefore could not directly bind it to an NSArrayController.
Steve
On 17 May 2013, at 19:52, Jeff Dyck <fsjjeff@gmail.com> wrote:
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 _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org https://lists.macosforge.org/mailman/listinfo/macruby-devel
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org https://lists.macosforge.org/mailman/listinfo/macruby-devel
participants (2)
-
Jeff Dyck
-
Steve Clarke