@@ -20,13 +20,9 @@ The `t-loading` directive shows content only during async operations. It automat
2020```
2121
2222<div t-data="{
23- loading: false,
24- startLoading() {
25- this.loading = true
26- setTimeout(() => { this.loading = false }, 2000)
27- }
23+ loading: false
2824}" class="p-6 bg-white dark: bg-gray-800 rounded-lg border border-gray-200 dark: border-gray-700 my-6 space-y-3">
29- <button t-click =" startLoading ()" :disabled =" loading " class =" px-6 py-2 bg-pine-600 text-white rounded-lg hover:bg-pine-700 disabled:opacity-50 " >
25+ <button t-click =" (function(){ loading = true; setTimeout(() => { loading = false }, 2000) }) ()" :disabled =" loading " class =" px-6 py-2 bg-pine-600 text-white rounded-lg hover:bg-pine-700 disabled:opacity-50 " >
3026 <span t-show="!loading">Start Loading</span>
3127 <span t-show="loading">Loading...</span>
3228 </button >
@@ -93,20 +89,9 @@ Show placeholder content while loading:
9389
9490<div t-data="{
9591 loading: false,
96- data: null,
97- async loadContent() {
98- this.loading = true
99- this.data = null
100- await new Promise(resolve => setTimeout(resolve, 2000))
101- this.data = {
102- title: 'Article Title',
103- author: 'John Doe',
104- content: 'This is the article content that was loaded from the server.'
105- }
106- this.loading = false
107- }
108- }" t-init="$context.loadContent()" class="p-6 bg-white dark: bg-gray-800 rounded-lg border border-gray-200 dark: border-gray-700 my-6 space-y-4">
109- <button t-click =" loadContent() " :disabled =" loading " class =" px-4 py-2 bg-pine-600 text-white rounded-lg hover:bg-pine-700 disabled:opacity-50 " >
92+ data: null
93+ }" t-init="(async function(){ loading = true; data = null; await new Promise(resolve => setTimeout(resolve, 2000)); data = { title: 'Article Title', author: 'John Doe', content: 'This is the article content that was loaded from the server.' }; loading = false; })()" class="p-6 bg-white dark: bg-gray-800 rounded-lg border border-gray-200 dark: border-gray-700 my-6 space-y-4">
94+ <button t-click =" (async function(){ loading = true; data = null; await new Promise(resolve => setTimeout(resolve, 2000)); data = { title: 'Article Title', author: 'John Doe', content: 'This is the article content that was loaded from the server.' }; loading = false; })() " :disabled =" loading " class =" px-4 py-2 bg-pine-600 text-white rounded-lg hover:bg-pine-700 disabled:opacity-50 " >
11095 Reload
11196 </button >
11297
@@ -151,18 +136,9 @@ Show loading progress:
151136
152137<div t-data="{
153138 progress: 0,
154- loading: false,
155- async load() {
156- this.loading = true
157- this.progress = 0
158- for (let i = 0; i <= 100; i += 10) {
159- await new Promise(resolve => setTimeout(resolve, 150))
160- this.progress = i
161- }
162- this.loading = false
163- }
139+ loading: false
164140}" class="p-6 bg-white dark: bg-gray-800 rounded-lg border border-gray-200 dark: border-gray-700 my-6 space-y-4">
165- <button t-click =" load ()" :disabled =" loading " class =" px-6 py-2 bg-pine-600 text-white rounded-lg hover:bg-pine-700 disabled:opacity-50 " >
141+ <button t-click =" (async function(){ loading = true; progress = 0; for (let i = 0; i <= 100; i += 10) { await new Promise(resolve => setTimeout(resolve, 150)); progress = i; } loading = false; }) ()" :disabled =" loading " class =" px-6 py-2 bg-pine-600 text-white rounded-lg hover:bg-pine-700 disabled:opacity-50 " >
166142 <span t-show="!loading">Start Loading</span>
167143 <span t-show="loading">Loading...</span>
168144 </button >
@@ -206,18 +182,9 @@ Show loading progress:
206182<div t-data="{
207183 step: 0,
208184 loading: false,
209- messages: [ '🔌 Connecting to server...', '📦 Loading data...', '⚙️ Processing...', '✨ Almost done...'] ,
210- async load() {
211- this.loading = true
212- for (let i = 0; i < this.messages.length; i++) {
213- this.step = i
214- await new Promise(resolve => setTimeout(resolve, 1200))
215- }
216- this.loading = false
217- this.step = 0
218- }
185+ messages: [ '🔌 Connecting to server...', '📦 Loading data...', '⚙️ Processing...', '✨ Almost done...']
219186}" class="p-6 bg-white dark: bg-gray-800 rounded-lg border border-gray-200 dark: border-gray-700 my-6 space-y-4">
220- <button t-click =" load ()" :disabled =" loading " class =" px-6 py-2 bg-pine-600 text-white rounded-lg hover:bg-pine-700 disabled:opacity-50 " >
187+ <button t-click =" (async function(){ loading = true; for (let i = 0; i < messages.length; i++) { step = i; await new Promise(resolve => setTimeout(resolve, 1200)); } loading = false; step = 0; }) ()" :disabled =" loading " class =" px-6 py-2 bg-pine-600 text-white rounded-lg hover:bg-pine-700 disabled:opacity-50 " >
221188 Start Process
222189 </button >
223190
@@ -246,17 +213,9 @@ Show loading state inline with content:
246213
247214<div t-data="{
248215 saving: false,
249- saved: false,
250- async save() {
251- this.saving = true
252- this.saved = false
253- await new Promise(resolve => setTimeout(resolve, 1500))
254- this.saving = false
255- this.saved = true
256- setTimeout(() => { this.saved = false }, 2000)
257- }
216+ saved: false
258217}" class="p-6 bg-white dark: bg-gray-800 rounded-lg border border-gray-200 dark: border-gray-700 my-6 space-y-3">
259- <button t-click =" save ()" :disabled =" saving " class =" px-6 py-3 bg-pine-600 text-white rounded-lg hover:bg-pine-700 disabled:opacity-50 flex items-center gap-2 " >
218+ <button t-click =" (async function(){ saving = true; saved = false; await new Promise(resolve => setTimeout(resolve, 1500)); saving = false; saved = true; setTimeout(() => { saved = false }, 2000); }) ()" :disabled =" saving " class =" px-6 py-3 bg-pine-600 text-white rounded-lg hover:bg-pine-700 disabled:opacity-50 flex items-center gap-2 " >
260219 <div t-show="saving" class="animate-spin rounded-full h-4 w-4 border-2 border-white border-t-transparent"></div>
261220 <span t-show="!saving && !saved">💾 Save Changes</span>
262221 <span t-show="saving">Saving...</span>
@@ -290,17 +249,9 @@ Disable buttons during loading:
290249
291250<div t-data="{
292251 submitting: false,
293- success: false,
294- async submit() {
295- this.submitting = true
296- this.success = false
297- await new Promise(resolve => setTimeout(resolve, 2000))
298- this.submitting = false
299- this.success = true
300- setTimeout(() => { this.success = false }, 3000)
301- }
252+ success: false
302253}" class="p-6 bg-white dark: bg-gray-800 rounded-lg border border-gray-200 dark: border-gray-700 my-6 space-y-3">
303- <button t-click =" submit ()" :disabled =" submitting " class =" w-full px-6 py-3 bg-pine-600 text-white rounded-lg hover:bg-pine-700 disabled:opacity-50 disabled:cursor-not-allowed font-medium flex items-center justify-center gap-2 " >
254+ <button t-click =" (async function(){ submitting = true; success = false; await new Promise(resolve => setTimeout(resolve, 2000)); submitting = false; success = true; setTimeout(() => { success = false }, 3000); }) ()" :disabled =" submitting " class =" w-full px-6 py-3 bg-pine-600 text-white rounded-lg hover:bg-pine-700 disabled:opacity-50 disabled:cursor-not-allowed font-medium flex items-center justify-center gap-2 " >
304255 <div t-show="submitting" class="animate-spin rounded-full h-5 w-5 border-2 border-white border-t-transparent"></div>
305256 <span t-show="!submitting && !success">Submit Form</span>
306257 <span t-show="submitting">Submitting...</span>
@@ -343,26 +294,14 @@ Handle multiple async operations:
343294 loadingUsers: false,
344295 loadingPosts: false,
345296 users: null,
346- posts: null,
347- async loadUsers() {
348- this.loadingUsers = true
349- await new Promise(resolve => setTimeout(resolve, 1500))
350- this.users = [ 'Alice', 'Bob', 'Charlie']
351- this.loadingUsers = false
352- },
353- async loadPosts() {
354- this.loadingPosts = true
355- await new Promise(resolve => setTimeout(resolve, 2000))
356- this.posts = [ 'Post 1', 'Post 2', 'Post 3']
357- this.loadingPosts = false
358- }
297+ posts: null
359298}" class="p-6 bg-white dark: bg-gray-800 rounded-lg border border-gray-200 dark: border-gray-700 my-6 space-y-4">
360299 <div class =" flex gap-2 " >
361- <button t-click="loadUsers ()" :disabled="loadingUsers" class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:opacity-50">
300+ <button t-click="(async function(){ loadingUsers = true; await new Promise(resolve => setTimeout(resolve, 1500)); users = ['Alice', 'Bob', 'Charlie']; loadingUsers = false; }) ()" :disabled="loadingUsers" class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:opacity-50">
362301 <span t-show="!loadingUsers">Load Users</span>
363302 <span t-show="loadingUsers">Loading...</span>
364303 </button>
365- <button t-click="loadPosts ()" :disabled="loadingPosts" class="px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 disabled:opacity-50">
304+ <button t-click="(async function(){ loadingPosts = true; await new Promise(resolve => setTimeout(resolve, 2000)); posts = ['Post 1', 'Post 2', 'Post 3']; loadingPosts = false; }) ()" :disabled="loadingPosts" class="px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 disabled:opacity-50">
366305 <span t-show="!loadingPosts">Load Posts</span>
367306 <span t-show="loadingPosts">Loading...</span>
368307 </button>
0 commit comments