|
Administrator
|
-- Gary Hockin <[hidden email]> wrote
(on Wednesday, 01 August 2012, 04:13 PM +0100): > When you use setVariables in a Zend\View\Model\ViewModel instance, it erases > any variables that has come before it. However, when you use setVariable it > simply appends the variable to anything previously set. Personally, I think > this is a little inconsistent and limiting. Here is my use case: > > Class FooController extends AbstractActionController > { > protected $viewModel; > > public function __construct() > { > $this->viewModel = new ViewModel(array( > 'somethingINeedInEveryAction' => 'Hello Mum' > )); > } > > public function indexAction() > { > return $this->viewModel->setVariables(array( > 'boo' => 'far', > 'cat' => 'dog', > )); > } > } > > > As you can see, I can't add variables en-masse if I don't want to overwrite > what I've set at construct time. > > My suggestions is either: > > Change setVariables so it doesn't overwrite the previously set variables, but > iterates and calls setVariable on each element, then create deleteVariables or > similar. > > or: > > Change setVariable so that it empties previous set variable and only sets what > you pass, and create "addVariable" and "addVariables" methods. I opt for the first idea. The liklihood that you actually want to replace the contents of the view model are pretty slim, and merging/adding/replacing values seems like a normal expectation. -- Matthew Weier O'Phinney Project Lead | [hidden email] Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc |
|
Hi,
addVariables - should add variables to the already existing variables and overwrite duplicates clearVariables - should clear all variables removeVariable - should remove a variable setVariable - should set the value of a variable. setVariables - should overwrite all variables :) Date: Wed, 1 Aug 2012 16:13:49 +0100 From: [hidden email] To: [hidden email] Subject: [zf-contributors] Zend\View\Model\ViewModel setVariables Hello! When you use setVariables in a Zend\View\Model\ViewModel instance, it erases any variables that has come before it. However, when you use setVariable it simply appends the variable to anything previously set. Personally, I think this is a little inconsistent and limiting. Here is my use case:
Class FooController extends AbstractActionController { protected $viewModel; public function __construct() { $this->viewModel = new ViewModel(array(
'somethingINeedInEveryAction' => 'Hello Mum' )); } public function indexAction() { return $this->viewModel->setVariables(array(
'boo' => 'far', 'cat' => 'dog', )); } } As you can see, I can't add variables en-masse if I don't want to overwrite what I've set at construct time.
My suggestions is either: Change setVariables so it doesn't overwrite the previously set variables, but iterates and calls setVariable on each element, then create deleteVariables or similar.
or: Change setVariable so that it empties previous set variable and only sets what you pass, and create "addVariable" and "addVariables" methods. Thanks. G |
|
Administrator
|
-- Walter Tamboer <[hidden email]> wrote
(on Wednesday, 01 August 2012, 06:36 PM +0200): > addVariables - should add variables to the already existing variables and > overwrite duplicates > clearVariables - should clear all variables > removeVariable - should remove a variable > setVariable - should set the value of a variable. > setVariables - should overwrite all variables This means changing the ModelInterface, and adding a lot of verbosity. I still stand by what I said previously -- in all liklihood, add vs merge will hit the YAGNI principle -- you'll likely only ever expect setVariable() and setVariables() to add/update the internal container. > ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ > Date: Wed, 1 Aug 2012 16:13:49 +0100 > From: [hidden email] > To: [hidden email] > Subject: [zf-contributors] Zend\View\Model\ViewModel setVariables > > Hello! > > When you use setVariables in a Zend\View\Model\ViewModel instance, it erases > any variables that has come before it. However, when you use setVariable it > simply appends the variable to anything previously set. Personally, I think > this is a little inconsistent and limiting. Here is my use case: > > Class FooController extends AbstractActionController > { > protected $viewModel; > > public function __construct() > { > $this->viewModel = new ViewModel(array( > 'somethingINeedInEveryAction' => 'Hello Mum' > )); > } > > public function indexAction() > { > return $this->viewModel->setVariables(array( > 'boo' => 'far', > 'cat' => 'dog', > )); > } > } > > > As you can see, I can't add variables en-masse if I don't want to overwrite > what I've set at construct time. > > My suggestions is either: > > Change setVariables so it doesn't overwrite the previously set variables, but > iterates and calls setVariable on each element, then create deleteVariables or > similar. > > or: > > Change setVariable so that it empties previous set variable and only sets what > you pass, and create "addVariable" and "addVariables" methods. > > Thanks. > > G -- Matthew Weier O'Phinney Project Lead | [hidden email] Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc |
|
So how would one implement the code mentioned in the question, Matthew?
On Wed, Aug 1, 2012 at 7:10 PM, Matthew Weier O'Phinney <[hidden email]> wrote: -- Walter Tamboer <[hidden email]> wrote |
|
Administrator
|
-- Tomáš Fejfar <[hidden email]> wrote
(on Thursday, 02 August 2012, 12:40 AM +0200): > So how would one implement the code mentioned in the question, Matthew? I actually created a PR for this yesterday. setVariables() now merges by default. However, an optional second argument to the method, $overwrite, will tell the model to replace the variables: $model->setVariables($variables, true); Thus, if you wanted to clear the container, pass an empty array, and true: $model->setVariables(array(), true); To remove a single variable, simply pass a null value to setVariable(). Yes, the key will still be present, but a null value is typically considered as "not present" in terms of view scripts. > On Wed, Aug 1, 2012 at 7:10 PM, Matthew Weier O'Phinney <[hidden email]> > wrote: > > -- Walter Tamboer <[hidden email]> wrote > (on Wednesday, 01 August 2012, 06:36 PM +0200): > > addVariables - should add variables to the already existing variables and > > overwrite duplicates > > clearVariables - should clear all variables > > removeVariable - should remove a variable > > setVariable - should set the value of a variable. > > setVariables - should overwrite all variables > > This means changing the ModelInterface, and adding a lot of verbosity. I > still stand by what I said previously -- in all liklihood, add vs merge > will hit the YAGNI principle -- you'll likely only ever expect > setVariable() and setVariables() to add/update the internal container. > > > > > ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ > > Date: Wed, 1 Aug 2012 16:13:49 +0100 > > From: [hidden email] > > To: [hidden email] > > Subject: [zf-contributors] Zend\View\Model\ViewModel setVariables > > > > Hello! > > > > When you use setVariables in a Zend\View\Model\ViewModel instance, it > erases > > any variables that has come before it. However, when you use setVariable > it > > simply appends the variable to anything previously set. Personally, I > think > > this is a little inconsistent and limiting. Here is my use case: > > > > Class FooController extends AbstractActionController > > { > > protected $viewModel; > > > > public function __construct() > > { > > $this->viewModel = new ViewModel(array( > > 'somethingINeedInEveryAction' => 'Hello Mum' > > )); > > } > > > > public function indexAction() > > { > > return $this->viewModel->setVariables(array( > > 'boo' => 'far', > > 'cat' => 'dog', > > )); > > } > > } > > > > > > As you can see, I can't add variables en-masse if I don't want to > overwrite > > what I've set at construct time. > > > > My suggestions is either: > > > > Change setVariables so it doesn't overwrite the previously set variables, > but > > iterates and calls setVariable on each element, then create > deleteVariables or > > similar. > > > > or: > > > > Change setVariable so that it empties previous set variable and only sets > what > > you pass, and create "addVariable" and "addVariables" methods. > > > > Thanks. > > > > G > > -- > Matthew Weier O'Phinney > Project Lead | [hidden email] > Zend Framework | http://framework.zend.com/ > PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc > > -- Matthew Weier O'Phinney Project Lead | [hidden email] Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc |
|
Ah, I understood it poorly before. I thought that you were against this feature in whole! :) I'm bit bugged by the
$model->setVariables(array(), true); part, because for me it feels natural to look for clearVariables() / unsetVariable(). But it's just convenience method which one can create in his own class ;)
On Thu, Aug 2, 2012 at 3:09 PM, Matthew Weier O'Phinney <[hidden email]> wrote: -- Tomáš Fejfar <[hidden email]> wrote |
| Powered by Nabble | Edit this page |
