Documentation ¶
Overview ¶
Package javascript converts go objects and types to javascript code, suitable for embedding in html or sending to the browser via a specialized ajax call.
Index ¶
Constants ¶
const JsonObjectType = "goraddObject"
JsonObjectType is used by the ajax processor in goradd.js to indicate that we are sending a special kind of object to the browser. These are things like dates, closures, etc. that are not easily represented by JSON.
Variables ¶
This section is empty.
Functions ¶
func NumberFloat ¶
func NumberFloat(i interface{}) float64
NumberFloat is a helper function to convert an expected float that is returned from a json Unmarshal as a Number, into an actual float64 without returning any errors. If there is an error, it just returns 0. Use this when you absolutely know you are expecting a float. Can convert strings too.
func NumberInt ¶
func NumberInt(i interface{}) int
NumberInt is a helper function to convert an expected integer that is returned from a json Unmarshal as a Number, into an actual integer without returning any errors. If there is an error, it just returns 0. Use this when you absolutely know you are expecting an integer. Can convert strings too.
func NumberString ¶
func NumberString(i interface{}) string
NumberString is a helper function to convert a value that might get cast as a Json Number into a string. If there is an error, it just returns 0. Use this when you absolutely know you are expecting a string.
func ToJavaScript ¶
func ToJavaScript(v interface{}) string
ToJavaScript will convert the given value to javascript such that it can be embedded in a browser. If it can, it will use the JavaScripter interface to do the conversion. Otherwise it generally follows json encoding rules. Strings are escaped. Nil pointers become null objects. String maps become javascript objects. To convert a fairly complex object, like a map or slice of objects, convert the inner objects to interfaces
Types ¶
type Arguments ¶
type Arguments []interface{}
func (Arguments) JavaScript ¶
Implements the JavaScripter interface
type Closure ¶
type Closure struct { // Body is the body javascript of the closure Body string // Args are the names of the arguments in the argument list of the closure Args []string }
Closure represents a javascript function pointer that can be called by javascript at a later time.
func (Closure) JavaScript ¶
JavaScript implements the JavsScripter interface and returns the closure as javascript code.
func (Closure) MarshalJSON ¶
MarshalJSON implements the json.Marshaller interface. The output of this is designed to be unpacked by the goradd javascript file.
type ClosureCall ¶
type ClosureCall struct { // Body is the body javascript of the closure Body string // Args are the names of the arguments in the argument list of the closure Args []string // Context is what will become the "this" argument inside of the closure when called. Specifying "this" will bring the "this" from the outer context in to the closure. Context string }
ClosureCall represents the result of a javascript closure that is called immediately.
func (ClosureCall) JavaScript ¶
func (c ClosureCall) JavaScript() string
JavaScript implements the JavsScripter interface and returns the closure as javascript code.
func (ClosureCall) MarshalJSON ¶
func (c ClosureCall) MarshalJSON() (buf []byte, err error)
Implements the json.Marshaller interface. The output of this is designed to be unpacked by the goradd javascript file.
type Function ¶
type Function struct { // The function name Name string // Function arguments. Strings will be quoted. Use a VarName object to output the name of a javascript variable. Args []interface{} // If given, the object in the window object which contains the function and is the context for the function. // Use dot '.' notation to traverse the object tree. i.e. "obj1.obj2" refers to window.obj1.obj2 in javascript Context string }
Function represents the result of a function call to a global function or function in an object referenced from global space. The purpose of this is to immediately use the results of the function call, as opposed to a Closure, which stores a pointer to a function that is used later.
func (Function) JavaScript ¶
func (Function) MarshalJSON ¶
*
- Returns this as a json object to be sent to qcubed.js during ajax drawing.
- @return mixed
type JavaScripter ¶
type JavaScripter interface {
JavaScript() string
}
JavaScripter specifies that an object can be converted to javascript (not JSON!). These objects should also be gob encodable and registered with gob, since they might be embedded in a control and need to be serialized.
type NoQuoteKey ¶
type NoQuoteKey struct {
Value interface{}
}
A value wrapper to specify a value in a map whose key should not be quoted when converting to javascript In some situations, a quoted key has a different meaning from a non-quoted key. For example, when making a list of parameters to pass when calling the jQuery $() command, (i.e. $j(selector, params)), quoted words are turned into parameters, and non-quoted words are turned into functions. For example, "size" will set the size attribute of the object, and size (no quotes), will call the size() function on the object.
func (NoQuoteKey) JavaScript ¶
func (n NoQuoteKey) JavaScript() string
Prevent using this as a general value.
type Undefined ¶
type Undefined struct { }
Undefined explicitly outputs as "undefined" in javascript. Generally, nil pointers become "null" in javascript, so use this if you would rather have an undefined value.