kingpin wrote
> 
> I am getting the exact same problem ( returning an empty object ).
> 
> This seems to be a very difficult task, for no reason... can anyone expand
> on the 'correct' way to properly return a rendered view script in json? 
> No template rendering, just the view script?  
> 
> We should have a better way to check to see if we want a partial to be
> returned, rather than the entire layout + the view... 
> 
> Think of all ajax apps, they simply just want to get a little content and
> return back to life... but the issue is, setting this up to have a
> fallback for browsers that do not support javascript...
> 
I've tried to dig into the zf code and I found that the JsonRenderer may
have a bug in the recurseModel function:
http://framework.zend.com/issues/browse/ZF2-183, i believe its related.
so if you open: library/Zend/View/Renderer/JsonRenderer.php [rev:719]

and change the function:
protected function recurseModel(Model $model)
    {
        $values = $model->getVariables();
        if (!$model->hasChildren()) {
            return $values;
        }

        $mergeChildren = $this->mergeUnnamedChildren();
        foreach ($model as $child) {
            $captureTo = $child->captureTo();
            if (!$captureTo && !$mergeChildren) {
                // We don't want to do anything with this child
                continue;
            }

            $childValues = $this->recurseModel($child);
            if ($captureTo) {
                // Capturing to a specific key
                $values[$captureTo] = $childValues;
            } elseif ($mergeChildren) {
                // Merging values with parent
                $values = array_replace_recursive($values, $childValues);
            }
        }
        return $values;
    }


to:

protected function recurseModel(Model $model)
    {
        $values = $model->getVariables();
        if (!$model->hasChildren()) {
            return $values;
        }
       *$values = array();*
        $mergeChildren = $this->mergeUnnamedChildren();
        foreach ($model as $child) {
            $captureTo = $child->captureTo();
            if (!$captureTo && !$mergeChildren) {
                // We don't want to do anything with this child
                continue;
            }

            $childValues = $this->recurseModel($child);
            if ($captureTo) {
                // Capturing to a specific key
                $values[$captureTo] = $childValues;
            } elseif ($mergeChildren) {
                // Merging values with parent
                $values = array_replace_recursive($values, $childValues);
            }
        }
        return $values;
    }

it works, but it returns a json as:* {"content":{"test":"json"}}* instead of
(what I assume it to be) *{"test":"json"}*

--
View this message in context: 
http://zend-framework-community.634137.n4.nabble.com/ZF2-contextSwitch-to-json-tp4418147p4439874.html
Sent from the Zend Framework mailing list archive at Nabble.com.

-- 
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to