1 Reply Latest reply on Jul 19, 2011 3:42 AM by Ondina D. F.

    Model in Robotleg is giving null error

    Shweta Gothe Level 1

      Dear All,

       

      I ahve defined a model in Context file :-

       

      injector.mapSingleton(AppModel);

       

      In a view file I am trying to assign values to the the Model but I am getting a null error.

       

       

      View file :-

       

      [Inject]

      public var _model:AppModel;

       

       

      private function init():void

      {

      _model.studName = "ABC"; // Getting null value here (_model =null)

      }

       

      AppModel.as :

       

      package

       

       

      {

       

      import org.robotlegs.mvcs.Actor;

       

       

      public class AppModel extends Actor

      {

       

      public function AppModel ()

      {

       

      super();

      }

       

       

      private var _studName:String;

       

       

      public function get studName():String {

       

      return _studName;

      }

       

       

      public function set studName(value:String):void {

      _studName= value;

      }

      }

      }

       

      Thanks,

      Shweta

        • 1. Re: Model in Robotleg is giving null error
          Ondina D. F.

           

          Hi Shweta,

           

           

           

          a better place to ask questions related to the Robotlegs framework is this:

           

          http://knowledge.robotlegs.org/discussions

           

           

           

          As far as I can see in your pasted code, you are trying to inject a Model into a View component.

           

          I don’t know how much you know about the implementation of the MVC pattern in Robotlegs, so just in case you are new to the framework, I'd recommend reading this:

           

           

          https://github.com/robotlegs/robotlegs-framework/wiki/best-practices

           

           

           

          You’ll see there that the View is described as follows:

           

          “The View tier is represented by the Mediator class. Classes that extend Mediator are used to handle framework interaction with View Components. A Mediator will listen for framework events, add event listeners to the View Components, and send framework events in response to events received from the View Components they are responsible for. This allows the developer to put application specific logic on the Mediator, and avoid coupling View components to specific applications.”

           

           

           

          You’ll also see that “Model” is a very complex topic   And also that a Model in Robotlegs is not the same as a VO (value object).

           

           

           

          If you really want to inject a Model into your Component while using a Mediator, one way of doing it is this:

           

           

           

          In your Context or a Command:

           

          import yourpath.models.SomeModel;

           

           

           

          injector.mapSingleton(SomeModel);

           

           

           

           

           

          SomeMediator.as

           

          package yourpath.views.mediators

           

          {

           

           

           

          import yourpath.views.components.SomeView;

           

          import org.robotlegs.core.IInjector;

           

          import org.robotlegs.mvcs.Mediator;

           

          public class SomeMediator extends Mediator

          {

           

          [Inject] public var injector:IInjector;

          [Inject] public var someView:SomeView;

           

          override public function onRegister():void

          {

          injector.injectInto(someView);

          }

           

          }

          }

           

           

          SomeView.mxml (Flex component)

           

          import yourpath.models.SomeModel;

           

          [Inject] public var someModel:SomeModel;

           

          private function someFunction():void

          {

          someModel.someProperty = “someValue”;

          }

           

           

           

          OR if you are not using Mediators, you can read Shaun’s answer in this thread:

           

          http://knowledge.robotlegs.org/discussions/questions/23-inject-in-views-how-to-and-whether -to

           

          OR if none of the above addressed your problem, you’re welcome to rephrase your question on the aforementioned forum. We are a very friendly community

           

          Ondina