示例#1
0
        public void Save(PresenterModel data, Action <string> next)
        {
            data.Item = new BlogPost {
                Title = data.Title, Description = data.Description, Id = data.Id
            };

            if (data.Item.Id > 0)
            {
                // simulate a slow save after editing an item

                /*
                 * setTimeout(function() {
                 *  actions.present(data, next);
                 * }, 9000);
                 */

                // slow save simulation not yet implemented in C#
                // save normally
                _present(data, next);
            }
            else
            {
                // proceed as normal when created a new item
                _present(data, next);
            }
        }
示例#2
0
 public void Edit(PresenterModel data, Action <string> next)
 {
     data.LastEdited = new BlogPost {
         Title = data.Title, Description = data.Description, Id = data.Id
     };
     _present(data, next);
 }
示例#3
0
        // The dispatch method decides whether an action can be dispatched
        // based on SAFE's context
        public void Dispatch(string action, PresenterModel data, Action <string> next)
        {
            this._logger.Info("dispatcher received request");
            bool dispatch        = false;
            var  lastStepActions = this._lastStep.Actions;

            this._logger.Info("dispatcher received request" + JsHelpers.JSON.stringify(data));
            this._logger.Info("lastStepActions            " + JsHelpers.JSON.stringify(lastStepActions));


            if (lastStepActions.Count == 0)
            {
                // action validation is disabled
                dispatch = true;
            }
            else
            {
                foreach (var lastStep in lastStepActions)
                {
                    this._logger.Info(lastStep.ToString());
                    if (lastStep.Action == action)
                    {
                        dispatch = true;
                        // tag the action with the stepid
                        // we want to enforce one action per step
                        // if the step does not match we should not dispatch
                        data.__actionId = lastStep.UId;
                        this._logger.Info("tagging action with            " + lastStep.ToString());

                        this._lastStep.Dispatched = action;
                    }
                }
            }

            if (!dispatch)
            {
                this._errorHandler(new { action = action, error = "not allowed" }.ToString());
            }
            else
            {
                if (this._actions.ActionList.ContainsKey(action))
                {
                    // dispatch action
                    this._logger.Info("invoking action            " + data.ToString());
                    this._actions.ActionList[action](data, next);
                }
                else
                {
                    this._errorHandler(new { action = action, error = "not found" }.ToString());
                }
            }
        }
示例#4
0
        public void Present(PresenterModel data, Action <string> next)
        {
            string actionId = data.__actionId ?? null;

            if (!this._blocked)
            {
                var lastStepActions = this._lastStep.Actions;
                this._logger.Info(lastStepActions.ToString());
                bool presentData = (lastStepActions.Count == 0);

                if (!presentData)
                {
                    // are we expecting that action?
                    foreach (var item in lastStepActions)
                    {
                        if (item.UId == actionId)
                        {
                            presentData = true;
                        }
                    }
                }
                if (presentData)
                {
                    Block();
                    if (!string.IsNullOrEmpty(data.__token))
                    {
                        this._model.__session = this._sessionManager.RehydrateSession(data.__token);
                        this._model.__token   = data.__token;
                    }
                    if (this._saveSnapshot != null)
                    {
                        // Store snapshot in TimeTravel
                        this._saveSnapshot(null, data.ToString());
                    }
                    this._model.Present(data, next);
                }
                else
                {
                    // unexpected actions
                    // possibly an action from previous step that needs to be blocked
                }
            }
            else
            {
                // ignore action's effect
                // this.logger({ blocked: true,data}) ; //todo
            }
        }
示例#5
0
        public void Present(PresenterModel data, Action <string> next)
        {
            if (data == null)
            {
                data = new PresenterModel(); //Implementation
            }

            if (data.DeletedItemId != 0)
            {
                var d = -1;

                foreach (var post in Posts)
                {
                    if (post.Id != 0 && post.Id == data.DeletedItemId)
                    {
                        d = post.Id;
                    }
                }

                if (d > 0)
                {
                    LastDeleted = Posts.ElementAt(d);
                }
            }


            if (data.LastEdited != null)
            {
                LastEdited = data.LastEdited;
            }
            else
            {
                // delete model.lastEdited; //TODO check
            }

            if (data.Item != null)
            {
                if (data.Item.Id > 0)
                {
                    // item has been edited
                    var indexer = 0;
                    foreach (var post in Posts)
                    {
                        if (post.Id > 0 && post.Id == data.Item.Id)
                        {
                            Posts[indexer] = data.Item;
                        }

                        indexer = indexer + 1;
                    }
                }
                else
                {
                    // new item
                    data.Item.Id = Posts.Max(x => x.Id) + 1;
                    Posts.Add(data.Item);
                }
            }

            //console.log(model);
            this.Render(this, next);
        }
示例#6
0
 public void Cancel(PresenterModel data, Action <string> next)
 {
     _present(data, next);
 }
示例#7
0
 public void Delete(PresenterModel data, Action <string> next)
 {
     data.DeletedItemId = data.Id;
     _present(data, next);
 }
示例#8
0
 /// <summary>
 /// Default Presenter Method.
 /// </summary>
 private static void DefaultPresent(PresenterModel data, Action <string> next = null)
 {
     // if this presenter is used, that means we forgot to specify one
     throw new NotImplementedException("Present function not properly initialized?");
 }