0

I would like to dynamically add/remove elements from a DukeScript page - in response to user actions.

Is this possible?

munyengm
  • 15,029
  • 4
  • 24
  • 34
  • Easy to do with jQuery event handlers. That's what jQuery was born for: DOM manipulation. – duffymo Sep 14 '15 at 11:29
  • Agreed, that's how I'd do it in a standard java webapp. But I think the point of Dukescript is to enable developers to do it all in Java. "DukeScript is a new technology for creating cross-platform mobile, desktop and web applications. DukeScript applications are plain Java applications that internally use HTML5 technologies and JavaScript for rendering. This way **developers only need to write clean Java code** and can still leverage the latest developments in modern UI technology. These are some of DukeScript Great Features. Explore the possibilities..." – munyengm Sep 14 '15 at 12:42
  • Curious to know - are you affiliated in any way with that project? My personal opinion is that I'd rather learn web technologies well. I only have so much bandwidth. No DukeScript for me. I'll be surprised if it's a success. – duffymo Sep 14 '15 at 12:44
  • Nope, not affiliated to it in anyway whatsoever. – munyengm Sep 14 '15 at 12:53
  • Incidentally... Isn't your statement a contradiction? "My personal opinion is that I'd rather learn web technologies well. I only have so much bandwidth". I guess you're assuming that DukeScript's learning curve is steeper than that of web technologies - which is probably true considering that DukeScript is still an immature technology. – munyengm Sep 14 '15 at 13:16
  • I'm saying that DukeScript will keep me writing Java so it can generate all that web code for me. I won't know how to do it on my own; I'll never learn it with DukeScript. Not a contradiction at all. – duffymo Sep 14 '15 at 13:17
  • Just for clarification: The idea of DukeScript is not primarily to create web sites. You can use it for that, but that's not the main use case. Instead DukeScript can be used to develop cross platform apps in Java. These apps run on Android, iOS, Desktop, and other platforms. The code is not transpiled to JavaScript but it's really running in a Java Virtual Machine. It only uses JavaScript and HTML for the view, because that's available everywhere. The idea is to make it easy to write cross platform single source apps. – monacotoni Sep 15 '15 at 05:34
  • The reason to use Java is that it has the best IDE support. The APIs are type save, so it's easy for the compiler and the IDE to spot errors and give you intelligent code completion. Also since the business code is native on android and close to native on iOS the performance is pretty good. – monacotoni Sep 15 '15 at 05:45
  • P.s.: I *am* affiliated with that project ;) – monacotoni Sep 15 '15 at 05:46

1 Answers1

1

Yes it's possible. There are different ways to achieve that. DukeScript uses knockout bindings in the html.

If you want to add remove an element from a dom you can embedd it in a conditional block:

http://knockoutjs.com/documentation/if-binding.html

Now depending on a property in your model the content of this block will be added to the dom. If for example your user ticks a checkbox, a boolean property of your model will be true, then the condition will apply and your dom elements will be added.

If you want for example to add new elements to a list, you can use the foreach binding:

http://knockoutjs.com/documentation/foreach-binding.html

You can bind it to an array type property of your model. Then for each of the elements the dom elements enclosed in the foreach block will be added. The data of this element can be bound to the properties of the list element.

and there are other ways... It really depends on what exactly you want. If you give an example of what you want to achieve it would help identify the best way of doing it.

monacotoni
  • 606
  • 5
  • 18