Return jquery-ui widget from function - coffeescript

I have a requirement wherein i need to return a widget instance from a coffeescript class method.
class Chart
constructor: (#chartData) ->
getChartType: ->
#chartData.type
getChartTitle: ->
#chartData.title
getChart: (context,ClickCallback) ->
#Need to create a chart widget instance and return
From the calling function i just want to do something like this
Chart c = new Chart(data)
object = c.getChart(#,#._onSeriesClick)
#.element.object()
Am I doing it the correct way? Is it possible to return a widget from a function or do i need to pass the element and initialize the widget within my coffeescript class?

When calling any function as new func(...) it will:
Create new empty object
Pass this new empty object as this to the function
Function then can modify this as needed (contructor will add methods and chartData to this)
If there are no explicit return statements - modified this object will be returned.
In case of CoffeeScript - it's better to leave things as they are: return an object of "class" Chart (keep in mind that "class" here is just a fancy name for convenience) and create a widget as you did in a object field.

Related

Using initialized String in asynchronous anonymous function in another page

how can you use String theFlag anywhere else outside of this anonymous function
I had to make it an anonymous function as I failed to point a separate asynchronous function into onPressed in a RaisedButton as it only worked with onChanged now i just need to pass String theFlag to another widget in another screen
please help and mind the beginner question
onPressed:() async {
Locale _temp = await setLocale(LanguageClass.languageList()[index].langCode);
BldrsApp.setLocale(context, _temp);
String theFlag = LanguageClass.languageList()[index].langFlag ; //I need to use this elsewhere
print(theFlag);
},
You can do it like this,
String iCanBeAcessed; //make this variable somewhere else, so that you can use it anywhere.
//Inside your anonymous function
iCanBeAcessed=theFlag;
How can i use that?
You can declare it outside a class, and can use it anywhere in your project.
Example,
//imports
String iCanBeAcessed;
class yourClassName{
//All your other functions and build method
Now, import the dart file( where you have declared the iCanBeAcessed ) in the following manner.
import dartFile.dart as flagString;
//Now, you can use this variable anywhere!
Text(flagString.iCanBeAcessed),

Why is an object instantiated after its declaration?

Okay so I'm trying to learn Dart by following flutter tutorials.
In the example below, right after the object is declared, an instance of itself is "created" (or at least I think so) and I don't understand why.
class CounterDisplay extends StatelessWidget {
CounterDisplay({this.count}); // What does this line do ?
final int count;
#override
Widget build(BuildContext context) {
return Text('Count: $count');
}
}
This code is from the tutorial found on this page:
https://flutter.dev/docs/development/ui/widgets-intro#changing-widgets-in-response-to-input
The line in question is this one :
CounterDisplay({this.count});
Could someone explain to me what does this line do and why it's here?
This doesn't create an instance of the object.
It is instead what we call a "constructor". Such syntax allows specifying custom parameters that need to be passed when creating the object.
See the dart documentation on constructors for more informations.
This make argument optional when create new object, or pass the variable name when creating object .

how to create a Form from an object of case class

I am using fold method of form as follows
def regSubmit = Action { implicit request =>
userForm.bindFromRequest.fold({
formWithErrors=>BadRequest(views.html.Error("Registration failed")( formWithErrors.errors))
},
{
userData=>Ok(views.html.regconf("Registration Successful")(**//here I want to send a Form, not data from the form**))
})
How can I create Form from a tuple or single variable, a class or a case class?
userForm will (usually?) be defined as a val, so immutable. It holds the mapping (this field name into a variable in this position of this type, ...) When you use bindFromRequest.fold you are not changing userForm, you are using the mapping information in userForm to generate a new instance of your case class, say userData (or a version of the form with errors in it). Each time you execute that method, you will get a new instance of userData.
userForm.fill(userData) returns a new form instance, a populated instance of the form, so also does not change userForm itself.

How do I override inherited methods when using JavaScript ES6/ES2015 subclassing

I have created a class that extends Array. I want to execute arbitrary code before calling the inherited push function.
class newArray extends Array{
//execute any logic require before pushing value onto array
this.push(value)
}
The solution I found was to create a new function in the subclass that has the same name as the inherited function. In this case push. Then inside the overriding function the inherited function is called via the super keyword.
class newArray extends Array{
push(value) {
//execute any logic require before pushing value onto array
console.log(`pushed ${value} on to array`)
super.push(value)
}
}
var array = new newArray
array.push('new Value')

Coffeescript setInterval in class

I started writing coffeescript last week, as I am programming a new Play20 site where coffeescript is the standard. I want to update a getData function in my class every 5 minutes, but the setInterval function does not bind to my class. Only the first time it calls getData, because the 'this' object is still reachable, as the setUpdateInterval() function is called from within the constructor.
But after the first call, the setInterval does not have any connection anymore with the Widget instance, and does not know what the this.getData() function is (and how to reach it).
Does someone know how to do it?
Here is my code:
class Widget
constructor: (#name) ->
this.setUpdateInterval()
getData: ->
console.log "get Data by Ajax"
setUpdateInterval: (widget) ->
setInterval( this.getData(), 3000000 )
Now here some Javascript magic is required. Reference
class Widget
constructor: (#name) ->
this.setUpdateInterval()
getData: ->
console.log "get Data by Ajax"
setUpdateInterval: (widget) ->
callback = #getData.bind(this)
setInterval( callback, 3000000 )
This will work in almost all browsers (guess which one not), so the
function will have to be bound differently. Some coffeescript magic:
callback = => #getData
The problem is that you are executing the function, instead of passing a reference to it.
Now, it sounds like you need to also keep the scope of the instance. do and => can help with that.
setUpdateInterval: (widget) ->
setInterval (do =>
#getData), 3000000
true
compiles to
Widget.prototype.setUpdateInterval = function(widget) {
var _this = this;
setInterval((function() {
return _this.getData;
})(), 3000000);
return true;
};
you will note the code executes a self invoking function, which return a function, that creates a closure around this, locking it into the scope of the callback (as _this)
Also note that you don't need to pass widget to the method (you aren't using it anywhere), and you would invoke this function in your constructor, to set up the interval. Whatever you do, you only want to call this method once. You could just put the contents of the function in your constructor.
Finally, since coffeescript returns the value of the last statement from all functions, I throw a true in there, but that might no be necessary.
This is handy in node as well. It's a varient of Tass's answer.
class Widget
constructor: (#options = {}) ->
#options.interval ?= 1000
#setInterval()
timer: ->
console.log 'do something'
setInterval: ->
cb = #timer.bind #
setInterval cb, #options.interval
w = new Widget()