0 Votes

Changes for page Todolist Macro

Last modified by Ryan C on 2025/04/30 07:54

From version 27.1
edited by Ryan C
on 2025/04/24 11:25
Change comment: There is no comment for this version
To version 28.1
edited by Ryan C
on 2025/04/24 11:28
Change comment: There is no comment for this version

Summary

Details

XWiki.JavaScriptExtension[0]
Code
... ... @@ -1,84 +1,69 @@
1 1  requirejs.config({
2 2   baseUrl: '${xwiki.getDocument("TodoLists.TodoListMacro").getURL("download")}/',
3 3   paths: {
4 - // Use existing jQuery if available, otherwise load 1.10.2
5 - 'jquery': window.jQuery ? 'empty:' : 'https://code.jquery.com/jquery-1.10.2.min'
4 + 'jquery-1.10.2': 'https://code.jquery.com/jquery-1.10.2.min'
6 6   },
7 - map: {
8 - // Map any module requiring 'jquery' to use the global jQuery
9 - '*': { 'jquery': window.jQuery ? 'jquery-global' : 'jquery' }
10 - },
11 11   shim: {
12 12   'ember-data': {
13 - deps: ['ember', 'jquery']
8 + deps: ['ember']
14 14   },
15 15   'ember': {
16 - deps: ['jquery']
11 + deps: ['jquery-1.10.2']
17 17   }
18 18   }
19 19  });
20 20  
21 -// Define a module that returns the global jQuery
22 -define('jquery-global', [], function() {
23 - return window.jQuery;
24 -});
25 -
26 -// Define empty module for when jQuery is already loaded
27 -define('empty:', [], function() {
28 - return window.jQuery;
29 -});
30 -
31 -require(['jquery', 'handlebars-v1.2.1', 'ember-data', 'ember'], function($) {
32 - // Check jQuery version compatibility with Ember
33 - var jQueryVersion = $.fn.jquery.split('.');
34 - var majorVersion = parseInt(jQueryVersion[0]);
35 - var minorVersion = parseInt(jQueryVersion[1]);
36 -
37 - console.log("Using jQuery version:", $.fn.jquery);
38 -
39 - // Verify jQuery version is compatible with Ember
40 - var isCompatible = (majorVersion === 1 && minorVersion >= 7 && minorVersion <= 10) ||
41 - (majorVersion === 2 && minorVersion === 0);
42 -
43 - if (!isCompatible) {
44 - console.error("Warning: Ember Views require jQuery 1.7, 1.8, 1.9, 1.10, or 2.0. Current version:", $.fn.jquery);
45 - // Try to load a compatible version if needed
46 - require(['https://code.jquery.com/jquery-1.10.2.min.js'], function(compatibleJQuery) {
47 - // Store the original jQuery
48 - var originalJQuery = window.jQuery;
49 - // Replace with compatible jQuery temporarily
50 - window.jQuery = compatibleJQuery;
51 - // Continue with the rest of the code
52 - initializeEmberApp(compatibleJQuery);
53 - // Restore original jQuery when done
54 - window.jQuery = originalJQuery;
55 - });
56 - } else {
57 - // jQuery version is compatible, proceed normally
58 - initializeEmberApp($);
59 - }
60 -
61 - function initializeEmberApp($) {
62 - // Create app
16 +// Load jQuery 1.10.2 specifically for Ember, using noConflict mode
17 +require(['jquery-1.10.2'], function(jQuery) {
18 + // Store the original jQuery and $ if they exist
19 + var originalJQuery = window.jQuery;
20 + var original$ = window.$;
21 +
22 + // Create a new jQuery instance in noConflict mode
23 + var emberJQuery = jQuery.noConflict(true);
24 +
25 + // Now load Ember with our specific jQuery
26 + require(['handlebars-v1.2.1', 'ember-data', 'ember'], function() {
27 + // Make emberJQuery available to Ember
28 + window.jQuery = emberJQuery;
29 + window.$ = emberJQuery;
30 +
31 + // Initialize Ember app
32 + initializeEmberApp(emberJQuery);
33 +
34 + // Restore original jQuery and $ when done
35 + window.jQuery = originalJQuery;
36 + window.$ = original$;
37 + });
38 +
39 + function initializeEmberApp(jQuery) {
40 + // Create app with specific jQuery instance
63 63   window.Todos = Ember.Application.create({
64 - rootElement: '#todoappdiv'
42 + rootElement: '#todoappdiv',
43 + jQuery: jQuery
65 65   });
66 66  
67 67   // Define the todolist store that will write to the TodoListsService
68 68   Todos.ApplicationAdapter = DS.Adapter.extend({
69 - namespace: 'api', // Moved inside the adapter definition
70 -
48 + serialize: function(record, options) {
49 + var serialized = record.record.toJSON();
50 + if (options && options.includeId) {
51 + serialized.id = record.id;
52 + }
53 + return serialized;
54 + },
55 +
71 71   createRecord: function(store, type, record) {
72 72   console.log("createRecord");
73 - var data = this.serialize(record, { includeId: true });
74 - var query = { create: "1", content: JSON.stringify(data) };
75 -
58 + var serialized = this.serialize(record, { includeId: true });
59 + var query = { create: "1", content: JSON.stringify(serialized) };
60 +
76 76   return new Ember.RSVP.Promise(function(resolve, reject) {
77 - var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
78 - XWiki.currentSpace + "." + XWiki.currentPage +
62 + var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
63 + XWiki.currentSpace + "." + XWiki.currentPage +
79 79   "&xpage=plain&outputSyntax=plain";
80 80   console.log("Creating record, URL:", url);
81 -
66 +
82 82   jQuery.getJSON(url, query).then(function(data) {
83 83   console.log("Create success:", data);
84 84   Ember.run(function() {
... ... @@ -92,7 +92,7 @@
92 92   });
93 93   });
94 94   },
95 -
80 +
96 96   deleteRecord: function(store, type, record) {
97 97   console.log("deleteRecord");
98 98   var recordArrays = store.recordArrayManager.recordArraysForRecord(record);
... ... @@ -101,22 +101,22 @@
101 101   }
102 102   return Ember.RSVP.resolve();
103 103   },
104 -
89 +
105 105   find: function(store, type, id) {
106 106   console.log("find");
107 107   return Ember.RSVP.resolve();
108 108   },
109 -
94 +
110 110   findAll: function(store, type, sinceToken) {
111 111   console.log("findAll");
112 112   var query = { since: sinceToken };
113 -
98 +
114 114   return new Ember.RSVP.Promise(function(resolve, reject) {
115 - var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
116 - XWiki.currentSpace + "." + XWiki.currentPage +
100 + var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
101 + XWiki.currentSpace + "." + XWiki.currentPage +
117 117   "&xpage=plain&outputSyntax=plain";
118 118   console.log("Finding all records, URL:", url);
119 -
104 +
120 120   jQuery.getJSON(url, query).then(function(data) {
121 121   console.log("FindAll success:", data);
122 122   Ember.run(function() {
... ... @@ -130,7 +130,7 @@
130 130   });
131 131   });
132 132   },
133 -
118 +
134 134   updateRecord: function(store, type, record) {
135 135   console.log("updateRecord");
136 136   var recordArrays = store.recordArrayManager.recordArraysForRecord(record);
... ... @@ -139,17 +139,17 @@
139 139   }
140 140   return Ember.RSVP.resolve();
141 141   },
142 -
127 +
143 143   saveAll: function(alldata) {
144 144   console.log("saveAll");
145 145   var query = { save: "1", content: JSON.stringify(alldata) };
146 -
131 +
147 147   return new Ember.RSVP.Promise(function(resolve, reject) {
148 148   var currentPage = XWiki.currentSpace + "." + XWiki.currentPage;
149 - var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
134 + var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
150 150   currentPage + "&xpage=plain&outputSyntax=plain";
151 151   console.log("Saving all records, URL:", url, "Page:", currentPage);
152 -
137 +
153 153   jQuery.getJSON(url, query).then(function(data) {
154 154   console.log("SaveAll success:", data);
155 155   Ember.run(function() {
... ... @@ -286,12 +286,12 @@
286 286   console.error("Failed to save todo:", error);
287 287   });
288 288   },
289 -
274 +
290 290   clearCompleted: function() {
291 291   var completed = this.filter(function(todo) {
292 292   return todo.get('isCompleted');
293 293   });
294 -
279 +
295 295   completed.forEach(function(todo) {
296 296   todo.deleteRecord();
297 297   todo.save();
... ... @@ -309,7 +309,7 @@
309 309   var remaining = this.get('remaining');
310 310   return remaining === 1 ? 'item' : 'items';
311 311   }.property('remaining'),
312 -
297 +
313 313   hasCompleted: function() {
314 314   return this.get('completed') > 0;
315 315   }.property('completed'),