Skip to content

Commit

Permalink
(templates): Fix inserting templates from into templateCache, drop su…
Browse files Browse the repository at this point in the history
…pport for Meteor versions prior to 0.8.3, fix tutorial's start to reflect the need to include angular-meteor angular module before start
  • Loading branch information
Urigo committed Jan 21, 2015
1 parent 53d9a79 commit db6c1ec
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 83 deletions.
44 changes: 39 additions & 5 deletions .docs/angular-meteor/client/views/steps/tutorial.step_00.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,49 @@ __`index.html`:__

<btf-markdown>

Now run the app. everything is the same. now let's start AngularJS:

inside index.html add an ng-app attribute on the DIV like that:
# AngularJS app

AngularJS apps are actually individual modules so now we are going to create our app module.

Create a new app.js file. now you can see another example of Meteor's power and simplicity - no need to include this file anywhere. Meteor will take care of it by going through all the files in the folder and including them automatically.

But Meteor's goal is to break down the barrier between client and server, and the code you write runs everywhere! (more on that later).
But we need Angular's power only in the client side, so how can we do that?

There are a few ways to tell Meteor to run a code only on the client/server/phone side, let's start with the simplest way - Meteor.isClient variable.

__`app.js`:__

if (Meteor.isClient) {
}

Now anything that will happen inside this if statement will run only on the client side.

So let's continue with starting our AngularJS application, we will call it "socially":

__`app.js`:__

if (Meteor.isClient) {
angular.module('socially',['angular-meteor']);
}

And name our application in the ng-app directive in index.html:

</btf-markdown>

</btf-markdown>
<pre><code>
&lt;<span class="hljs-operator">div</span> ng-app ng-<span class="hljs-built_in">include</span>=<span class="hljs-string">"'index.tpl'"</span>&gt;&lt;/<span class="hljs-operator">div</span>&gt;
&lt;<span class="hljs-operator">div</span> ng-app=<span class="hljs-string">"socially"</span> ng-<span class="hljs-built_in">include</span>=<span class="hljs-string">"'index.tpl'"</span>&gt;&lt;/<span class="hljs-operator">div</span>&gt;
</code></pre>
<btf-markdown>
<btf-markdown>


What we did here is to declare a new angular module named 'socially' and making it dependant on the 'angular-meteor' module (that we included in the first step).

Now run the app.

everything is the same.

and now inside our index.tpl let's use Angular:

Expand Down
47 changes: 2 additions & 45 deletions .docs/angular-meteor/client/views/steps/tutorial.step_02.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -66,53 +66,10 @@ We have added a new directive, called ng-controller, which attaches the PartiesL
* The expressions in double-curly-braches ( {{party.name}} and {{party.description}} ) denote bindings, which are referring to our application model, which is set up in our PartiesListCtrl controller.


# AngularJS app

Now we are going to create our own AngularJS client app with client controller and model.

First, let's create our app.

Create a new app.js file. now you can see another example of Meteor's power and simplicity - no need to include this file anywhere. Meteor will take care of it by going through all the files in the folder and including them automatically.

But Meteor's goal is to break down the barrier between client and server, and the code you write runs everywhere! (more on that later).
But we need Angular's power only in the client side, so how can we do that?

There are a few ways to tell Meteor to run a code only on the client/server/phone side, let's start with the simplest way - Meteor.isClient variable.

__`app.js`:__

if (Meteor.isClient) {
}

Now anything that will happen inside this if statement will run only on the client side.

So let's continue with starting our AngularJS application, we will call it "socially":

__`app.js`:__

if (Meteor.isClient) {
angular.module('socially',['angular-meteor']);
}

And name our application in the ng-app directive in index.html:

</btf-markdown>

<pre><code>
&lt;<span class="hljs-operator">div</span> ng-app=<span class="hljs-string">"socially"</span> ng-<span class="hljs-built_in">include</span>=<span class="hljs-string">"'index.tpl'"</span>&gt;&lt;/<span class="hljs-operator">div</span>&gt;
</code></pre>
<btf-markdown>


What we did here is to declare a new angular module named 'socially' and making it dependant on the 'angular-meteor' module (that we included in the first step).

Then we told our application to start our angular module on startup.


# Model and Controller

Now let's create our PartiesListCtrl controller and place data in it.
Now we are going to create our controller and model.
We start with PartiesListCtrl controller and place data in it.


__`app.js`:__
Expand Down
46 changes: 14 additions & 32 deletions modules/angular-meteor-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,29 @@ var angularMeteorTemplate = angular.module('angular-meteor.template', []);
angularMeteorTemplate.run(['$templateCache',
function ($templateCache) {
angular.forEach(Template, function (template, name) {
if (name.charAt(0) != "_" && name != "prototype" && name != "loginButtons") { // Ignores templates with names starting with "_"
$templateCache.put(name, '<ng-template name="' + name + '"></span>');
if (
name.charAt(0) != "_" &&
name != "prototype" &&
name != "loginButtons" &&
name != "instance" &&
name != "currentData" &&
name != "parentData" &&
name != "body" &&
name != "registerHelper") { // Ignores templates with names starting with "_"

$templateCache.put(name, '<ng-template name="' + name + '"></ng-template>');
}
});
}
]);

angularMeteorTemplate.directive('ngTemplate', ['$templateCache',
function ($templateCache) {
angularMeteorTemplate.directive('ngTemplate', [
function () {
return {
restrict: 'E',
scope: false,
template: function (element, attributes) {

// Check if version prior 0.8.3
if (Template[attributes.name].render){
var name = attributes.name,
template = Template[name],
templateRender = Blaze.toHTML(template),
templateString = null;

// Check for nested templates in the render object and replace them with the equivalent ngTemplate directive.
angular.forEach(templateRender, function (v, k) {
if (angular.isObject(v)) {
if (v._super) {
var transcludeTemplateName = v._super.kind.replace('Template_', '');
templateRender[k] = new HTML.Raw($templateCache.get(transcludeTemplateName));
}
}
});

if (angular.isDefined(template)) {
templateString = UI.toHTML(templateRender);
} else {
throw new ReferenceError("There is no Meteor template with the name '" + name + "'.");
}

return templateString;
} else {
return Blaze.toHTML(Template[attributes.name]);
}
return Blaze.toHTML(Template[attributes.name]);
},
link: function (scope, element, attributes) {
var name = attributes.name,
Expand Down
2 changes: 1 addition & 1 deletion test/angular/unit/angular-meteor-template.Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Given the Template Service', function() {
it('should ignore templates with names "prototype", "loginButtons" or starting with _', function() {

var output1 = $templateCache.get('other');
expect(output1).toEqual('<ng-template name="other"></span>');
expect(output1).toEqual('<ng-template name="other"></ng-template>');

var output2 = $templateCache.get('_x');
expect(output2).toBeUndefined();
Expand Down

0 comments on commit db6c1ec

Please sign in to comment.