Skip to content

Commit

Permalink
Added ability to use across multiple views
Browse files Browse the repository at this point in the history
An array of views can now be set within the config file.
  • Loading branch information
J5Dev - Chris Gooding authored and J5Dev - Chris Gooding committed Jul 23, 2014
1 parent 58b4e02 commit 1508a5d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ return [
| Set this value to the name of the view (or partial) that
| you want to prepend the JavaScript variables to.
|
| Accepts a string or an array
|
*/
'bind_js_vars_to_this_view' => 'hello',

Expand All @@ -103,6 +105,15 @@ return [
You need to update this file to specify which view you want the new transformed JavaScript variables to be prepended to. Typically, your footer is a good place for this.
If you include something like a `layouts/partials/footer` partial, where you store your footer and script references, then make the `bind_js_vars_to_this_view` key equal to that path. Behind the scenes, the Laravel implementation of this package will listen for when that view is composed, and essentially paste the JS variables within it.

If wanting to use this across multiple views, simply set the value to an array, passing in the names of all views you want to utilise the Vars, such as...

```
'bind_js_vars_to_this_view' => [
'hello',
'someotherview',
]
```

#### js_namespace

By default, all JavaScript vars will be nested under the global `window` object. You'll likely want to change this. Update the
Expand Down
15 changes: 12 additions & 3 deletions src/Laracasts/Utilities/JavaScript/LaravelViewBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,19 @@ function __construct(Dispatcher $event, $viewToBindVariables)
*/
public function bind($js)
{
$this->event->listen("composing: {$this->viewToBindVariables}", function() use ($js)
if ( ! is_array($this->viewToBindVariables))
{
echo "<script>{$js}</script>";
});
$this->viewToBindVariables = [$this->viewToBindVariables];
}

foreach ($this->viewToBindVariables as $viewVariable)
{
$this->event->listen("composing: {$viewVariable}", function () use ($js)
{
echo "<script>{$js}</script>";
});
}

}

}

0 comments on commit 1508a5d

Please sign in to comment.