Initialize (and performing functions once upon launch)

  2 posts   Feedicon  
Replies: 1 - Last Post: February 07, 2010 16:24
by: Logan Barnett
showing 1 - 2 of 2
Posted: February 06, 2010 18:04 by DRobinson
First off I'm going to mention I'm extremely new to Ruby (/JRuby) , monkeybars, and MVC architectures in general. In fact I'm just a student that thought it would be fun to learn Ruby for a project rather than be like just about everyone else using Java. And I’m certainly glad I did; I’ve since fallen in love with Ruby. I've been able to figure out just about everything I've needed to know so far (from monkeybars interactions with jcomboboxes, jlists, etc. to some nice ruby ways of file manipulation and downloading files via http). Basically what I'm trying to say is if I seem clueless or inept, I probably am just that.

The one thing I'm having trouble doing is getting something to run just once when the program is launched. At first it seemed like it would be simply a matter of putting the function call in the model's def initialize, but this seems to be called every time I use update_model in the controller (which I need to do to get text inputs or combobox changes, etc. Unless there's a better way to do that which i would also be interested in).

I've tried making an "initialized" variable, checking if it's nil, then after the function making it =true. This didn't work, it still gets itself into the if statement without fail (I checked using puts before and after the function, displayed nil and true respectively but I guess it doesn't stick upon updates). Or even just do the few things I need to do in main.rb, even if I feel there's probably a better solution.

I feel like I might be going about it wrong. I might have to do it from the controller (at first setting view/model to like a splash screen or something, having it do the one time stuff like checking for file updates done in that model, then afterwards setting to the view and model to the current ones).

I don't know. I mean this isn't hugely essential; I could get away with having the function called every time. It just doesn't seem right to go through initialize each time. I feel like maybe I'm just under the wrong impression about the point of initialize and maybe I'm looking for something else. Anyways, any ideas?

Thank you for your time;
Posted: February 07, 2010 16:24 by Logan Barnett

Your controller has a reference to a model (which you declared using set_model). Each time you call view_state.model/view_model, we create a copy of your model and populate it based on your view mappings. This lets you cherry pick what data gets applied to the actual model. The copy is transient and thrown away after your event is finished.

Instead of having heavy logic in your constructor, I recommend some kind of load method on your model. In the controller's load, call model.load once.

Monkeybars is bring your own model, but there are some terms of engagement we have. If you can't do something like above, consider making a middle-man model. We've done this for active record, where certain kinds of data just didn't make sense living on the direct Monkeybars model.

Replies: 1 - Last Post: February 07, 2010 16:24
by: Logan Barnett
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2014, Oracle Corporation and/or its affiliates
(revision 20160708.bf2ac18)
Please Confirm