Skip to content

Commit ef430d0

Browse files
authored
fix: Merge pull request #105 from leonardobazico/master
fix: Make performLocalElementUpdates wait for document ready
2 parents 2cf5ff5 + f37f31a commit ef430d0

File tree

2 files changed

+89
-22
lines changed

2 files changed

+89
-22
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,60 @@ Add these lines to header of index.html
286286
<script type="text/javascript" src="vendor/jquery/dist/jquery.min.js"></script>
287287
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
288288
```
289+
#### Installing & configuring angular2-materialize in projects created with angular-cli@webpack (1.0.0-beta.11-webpack.8)
290+
291+
Install MaterializeCSS and angular2-materialize from npm
292+
```
293+
npm install materialize-css --save
294+
npm install angular2-materialize --save
295+
```
296+
297+
Jquery is required
298+
```
299+
npm install jquery@^2.2.4 --save
300+
```
301+
302+
add scripts in angular-cli.json
303+
```
304+
"scripts": [
305+
"../node_modules/jquery/dist/jquery.js",
306+
"../node_modules/materialize-css/dist/js/materialize.js"
307+
],
308+
```
309+
310+
Import MaterializeModule in app.module.ts
311+
```
312+
import { BrowserModule } from '@angular/platform-browser';
313+
import { NgModule } from '@angular/core';
314+
import { FormsModule } from '@angular/forms';
315+
import { HttpModule } from '@angular/http';
316+
317+
import { MaterializeModule } from 'angular2-materialize';
318+
319+
import { AppComponent } from './app.component';
320+
321+
@NgModule({
322+
declarations: [ AppComponent ],
323+
imports: [
324+
BrowserModule,
325+
FormsModule,
326+
HttpModule,
327+
MaterializeModule
328+
],
329+
providers: [],
330+
bootstrap: [AppComponent]
331+
})
332+
export class AppModule { }
333+
```
334+
335+
Add these lines to header of index.html
336+
```
337+
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
338+
```
339+
340+
Import materialize.css in style.css
341+
```
342+
/* You can add global styles to this file, and also import other style files */
343+
@import "../node_modules/materialize-css/dist/css/materialize.css";
344+
```
289345

src/materialize-directive.ts

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class MaterializeDirective implements AfterViewInit,DoCheck,OnChanges,OnD
3232
private _params:[any] = null;
3333
private _functionName:string = null;
3434
private previousValue = null;
35+
private _waitFunction: any = { };
3536

3637
private changeListenerShouldBeAdded = true;
3738

@@ -156,37 +157,47 @@ export class MaterializeDirective implements AfterViewInit,DoCheck,OnChanges,OnD
156157

157158
this.performLocalElementUpdates();
158159
}
159-
160+
160161
private performLocalElementUpdates(functionName=this._functionName) {
161-
if (functionName) {
162-
const jQueryElement = $(this._el.nativeElement);
163-
if (jQueryElement[functionName]) {
164-
if (this._params) {
165-
if (this._params instanceof Array) {
166-
jQueryElement[functionName](...this._params);
167-
} else {
168-
throw new Error("Params has to be an array.")
169-
}
170-
} else {
171-
jQueryElement[functionName]();
172-
}
173-
} else {
174-
// fallback to running this function on the global Materialize object
175-
if (Materialize[functionName]) {
162+
if (this._waitFunction[functionName]) {
163+
return;
164+
}
165+
166+
this._waitFunction[functionName] = true;
167+
168+
$(document).ready(() => {
169+
this._waitFunction[functionName] = false;
170+
171+
if (functionName) {
172+
const jQueryElement = $(this._el.nativeElement);
173+
if (jQueryElement[functionName]) {
176174
if (this._params) {
177175
if (this._params instanceof Array) {
178-
Materialize[functionName](...this._params);
176+
jQueryElement[functionName](...this._params);
179177
} else {
180-
throw new Error("Params has to be an array.")
178+
throw new Error("Params has to be an array.");
181179
}
182180
} else {
183-
Materialize[functionName]();
181+
jQueryElement[functionName]();
184182
}
185183
} else {
186-
throw new Error("Couldn't find materialize function ''" + functionName + "' on element or the global Materialize object.");
184+
// fallback to running this function on the global Materialize object
185+
if (Materialize[functionName]) {
186+
if (this._params) {
187+
if (this._params instanceof Array) {
188+
Materialize[functionName](...this._params);
189+
} else {
190+
throw new Error("Params has to be an array.");
191+
}
192+
} else {
193+
Materialize[functionName]();
194+
}
195+
} else {
196+
throw new Error("Couldn't find materialize function ''" + functionName + "' on element or the global Materialize object.");
197+
}
187198
}
188199
}
189-
}
200+
});
190201
}
191202

192203
private isTooltip(){
@@ -218,4 +229,4 @@ export class MaterializeDirective implements AfterViewInit,DoCheck,OnChanges,OnD
218229
event.initEvent(eventType, true, true);
219230
}
220231
target.dispatchEvent(event);
221-
};*/
232+
};*/

0 commit comments

Comments
 (0)