What does page() do? - page.js

In the first example on https://visionmedia.github.io/page.js/...
page('/', index)
page('/user/:user', show)
page('/user/:user/edit', edit)
page('/user/:user/album', album)
page('/user/:user/album/sort', sort)
page('*', notfound)
page() // what does this do?

page() is equivalent to page.start().
source

Related

How to Conditionally reverse a list with in a compareBy in Kotlin

I have the following code
val sortMethod = compareBy<Item> {
when (model.preferences.getSortMethod()) {
TITLE -> it.getTitle().toLowerCase(Locale.getDefault())
DATE -> it.getSortableDateString()
AUTHOR -> it.getAuthor().toLowerCase(Locale.getDefault())
DATE_ADDED -> it.getSortableDateAddedString()
}
}.thenBy { it.getTitle().toLowerCase(Locale.getDefault()) }
which i then use on Lists like so
myItems.sortedWith(sortMethod)
I like this solution and I think it's very elegant. However, i would like to add the ability to sort ascendingly/descendingly as well.
So I am asking how do I add a conditional .reverse() to my sortMethod? I am very reluctant to put a if condition on every one of my sort calls and would like to get it all done in the sortMethod logic
You can do it like this:
list.sortedWith(if (sortAscending) sortMethod else sortMethod.reversed())
I suggest removing Locale.getDefault() since toLowerCase() is equivalent to toLowerCase(Locale.getDefault()).
private fun List<Item>.sort() : List<Item>{
if (model.preferences.isSortedAscendingly()){
return this.sortedWith(sortMethod)
}
return this.sortedWith(sortMethod).reversed()
}
Decided my solution will be using the above extension function, but if anyone has a better way i would love to hear.
Use let()?
private fun List<Item>.sort() = sortedWith(sortMethod).let {
if (model.preferences.isSortedAscendingly())
it
else
it.reversed()
}

view does not appear, only data appears

i want to passing data to view
my controller
public function create()
{
$maxcode=Product::selectRaw('MAX(RIGHT(product_code, 3)) as max')->first();
$prx='P2018-';
if($maxcode->count()>0)
{
$tmp = ((int)$maxcode->max)+1;
$newcode = $prx.sprintf("%03s", $tmp);
}
else
{
$newcode = $prx."P2018-001";
}
return $newcode;
return view('product.create', $newcode);
}
my view
...other code...
#foreach($newcode as $nc)
{{$nc->newcode}}
#endforeach
---other code...
the code works well, but the only results appear, the other code on the view does not work.
someone might be able to help me
remove return $newcode;
return view('product.create')->with('newcode',$newcode);
$newcode variable is not an array, you can not use foreach.
use {{$newcode}} in blade to display value of variable.
remove return $newcode; line and change your return function like this,
return view('product.create', compact('newcode'));

How to lowercase first letter of Textmate snippet input?

I have shortcut for creating getter method and here is my snippet code right now:
public function get${1:PropertyName}() {
return \$this->${1:propertyName};
}
$0
Output I'm looking for:
public function getAreaCode() {
return $this->areaCode;
}
So the question is, how to automatically transform input's first letter into lowercase, but only on the second line?
You can do a regexp match that matches the first character and modifies it like this:
public function get${1/./\u/}() {
return \$this->${1:propertyName};
}
$0
I've used this, to also add the property and setting the scope with dropdown:
${2|private,protected,public|} \$${1};
${3|public,protected,private|} function get${1/./\u$0/}() {
return \$this->${1:propertyName};
}
${3} function set${1/./\u$0/}(\$value) {
\$this->${1} = \$value;
return \$this;
}
$0
See Transformation section on Macromates for more.

TypeScript refactoring. Update Collection

I have a lot of foreach in TypeScript, some of them:
$.each(obj.triggers, function (index, value) {
(<any>sc.triggers).push(Trigger.objectToTrigger(value));
});
$.each(obj.notStartTriggers, function (index, value) {
(<any>sc.notStartTriggers).push(Trigger.objectToTrigger(value));
});
How I can refactor this foreach's? I need one method for this foreach.
Thanks to all. I need update sc.notStartTriggers collection, not add(push)?
So you have a property on obj and the same property on sc that you want to loop on using Trigger. So here is a function to take these as parameters:
function process(obj,sc,property:string,Trigger){
$.each(obj[property], function (index, value) {
(sc[property]).push(Trigger.objectToTrigger(value));
});
}
process(obj,sc,"triggers",Trigger);
process(obj,sc,"notStartTriggers",Trigger);
Note that the following are equivalent in javascript / typescript:
x.asdf
x["asdf"]
The main purpose of a refactor on your code would probably be to remove the casting, rather than to remove the slight duplication.
Also, you don't need to go as far as any, as the operations you are looking for are from an array, so you could use any[] which makes at least that much known.
I have stretched Robert C Martin's rule on the number of parameters by allowing two arguments to be passed to my method :) - the arguments are in the order implied by the method name, add trigger to array.
function addTriggerToArray(trigger: any, triggerArray: any[]) {
triggerArray.push(trigger);
}
$.each(obj.triggers, function (index, value) {
addTriggerToArray(Trigger.objectToTrigger(value), sc.triggers);
});
$.each(obj.notStartTriggers, function (index, value) {
addTriggerToArray(Trigger.objectToTrigger(value), sc.notStartTriggers);
});
To update existing items you would use...
function updateTrigger(trigger: any, index: number, triggerArray: any[]) {
triggerArray[index] = trigger;
}
$.each(obj.triggers, function (index, value) {
updateTrigger(Trigger.objectToTrigger(value), index, sc.triggers);
});
$.each(obj.notStartTriggers, function (index, value) {
updateTrigger(Trigger.objectToTrigger(value), index, sc.notStartTriggers);
});

pass function to another and call by name

<a id="aHw" href="#" callbackName="helloworld">test</a>
...
<script>
function helloworld() { alert('hello world'); }
</script>
...
question ; how can i produce callBack to pass another function
<script>
...
var cbName = $('#aHw').attr('callbackName');
foo( passfunction ); //How???
...
</script>
<script>
function foo(callBack)
{
callBack(); // call hello world.
}
</script>
thanks in advance.
A function in JavaScript is just an object.
The question(s) don't make terribly much sense to me, but consider the following:
function fn1 () {
alert("fn1")
}
function doIt(cb) {
cb()
}
// direct -- to show point in general
// fn1 evaluates to the function-object, which is then passed
doIt(fn1)
// lookups up fn1 by name, then passes that function-object
// the value "fn1" can be any arbitrary string, such as that which
// was stored in the attr:
// e.g. doIt(window[cbName])
doIt(window["fn1"])
// anon function to iterate sameness
// the anon function returns a new function-object
doIt(function () { alert("anon") })
Happy coding.
Ok. So to have an anchor do something on MouseOver, you'd use this code:
<a id="aHw" href="#" onmouseover="doSomething()">test</a>
You can pass a function to another function this way:
function callSomeFunction( fn )
{
fn();
}
callSomeFunction( alert );
Or you can pass an anonymous function to the above:
callSomeFunction( function(){ alert( "Finally! A message!" ); } );
If you're trying to pass the name of a function as a string (which is a fundamentally bad idea and a terrible risk and hard to debug and DON'T DO IT), then you can use eval:
function callNamedFunction( fn )
{
eval(fn)()
}
Or you might be able to get away with:
function callNamedFunction( fn )
{
(window[fn])()
}
foo( Function('return ' + cbName)() )
I think that's what your after..
But if it's in the browser, and you know that the callback is a global object, you could do..
foo(window[cbName])
Well, if nothing else helps, eval() will:
function foo( callBack ) {
eval( callBack + '()' );
}
If you know where the function is defined (e.g window, or custom namespace) you can invoke it by the string name. Otherwise you would have to eval (bad idea). Also, use data-attributes.
test
...
// invoke runs a function by name in the provided context, or window
function invoke(cbname, context){
return (context || window)[cbname].call();
}
// get attribute through the data method
var funcName = $('a').data('callback');
// run callback
var result = invoke(funcName);

Resources