»

Backbone.js notes

Coding, Log — Tags: , — Posted by Starck on October 18, 2013


Backbone.Events

Method Note
object.on(event, callback, [context]) 註冊監聽一個事件, 並指定觸發 callback, 如果需要在 callback function 中改變 this 的對象, 將對象傳入 context
object.off([event], [callback], [context]) 取消監聽事件, 不指定事件將取消所有的事件

Backbone Event Testing


Backbone.Model

Properties / Method Note
constructor() You can use Backbone.Model.apply(this, arguments) to invoke constructor function of parent class
initialize() If you definean initialize function, it’ll be invoked when the model is created.
defaults Remember that in JavaScript, objects are passed by reference, so if you include an object as a default value, it will be shared among all instances. Instead, define defaults as a function.
attributes If you’d like to retrieve and munge a copy of the model’s attributes, use _.clone(model.attributes) instead.
validate(attrs, options) By default validate is called before save, but can also be called before set if {validate:true} is passed. Return your custom error message, and it will be stored to validateError
isValid() Run validate() to check the model state.
validationError The value returned by validate during the last failed validation.
set()
get()
set(‘attr’, ‘value’) or set({ attr: value})
destroy() You can use _.invoke() to call destroy function for a batch of models. ex: _.invoke(modelsArray, 'destroy')


Backbone.Collection

  • create({ attr: value })
  • fetch()
Properties / Method Note
model
url
models Raw access to the JavaScript array of models inside of the collection.
add
create
reset

Project Template


Backbone.View

Properties / Method Note
tagName
id
className
attributes
el and $el
events
render() A good convention is to return this at the end of render to enable chained calls.


Backbone JS Data Types Primer


Backbone.LocalStorage


Example: TodoList

Backbone TodoList

ExpressionEngine Notes

Coding, Log — Tags: , — Posted by Starck on October 17, 2013


Template

取得 channel 的文章, 並顯示 channel custom field 值

if tag

Custom Tags

編寫 Plugin file:

假設我們今天要建立的 Plugin 名稱叫作 Custom

先在 third_party 資料夾底下建立 custom 資料夾 命名為 pi.custom.php .

若發現無法使用 $this->EE->TMPL 時

$this->EE->load->library('template',null,'TMPL');

使用 Template::parse_variables_row

使用 Template::parse_variables

Module

  • upd.module_name.php
  • mcp.module_name.php

mcp 就是 module 的 controller, 假設一個被請求的 url 如下:

admin.php?D=cp&C=addons_modules&M=show_module_cp&module=beyond&method=

C=addons_modules 代表目前的 Controller 是 expression/controllers/cp/addons_modules.php
M=show_module_cp 代表使用該 controller 的 show_module_cp 這個 method
module=beyond 代表被呼叫的 module id名稱為 beyond (third_party/{module_id})
method= 執行該 module controller 的哪一個 method, 未指定預設會導向 index method

透過指定 method 這個 GET 變數,將改變 module controller 呼叫不同的 method, 另外你也可以外部讀取 template 產生不同的 view, 將這些 templates 放置在 module/views 裡, 透過以下在 module controller 的 method 中回傳:

return $this->EE->load->view('template_name', $vars, TRUE);

載入自定義 javascript

  • mod.module_name.php
  • language/english/module_name_lang.php

Language 檔案定義 $lang 語系變數:

語系中 module_name_module_name 和 module_name_module_description 會出現在 MCP 的安裝 modules 列表上

How to create or update a Channel Field?

$this->EE->api_channel_fields->update_field((array) $field_data);

returns: (string) The field_id of the updated/created field.

To Enable Mysql General Log

Log — Tags: — Posted by Starck on October 17, 2013


Edit my.cnf:

You can also rename the general query log file at runtime by disabling the log:

SET GLOBAL general_log = 'OFF';

With the log disabled, rename the log file externally; for example, from the command line. Then enable the log again:

SET GLOBAL general_log = 'ON';


Mysql Reference

How to recursively give directories or files access permissions?

Log — Tags: , , , — Posted by Starck on October 8, 2013


To recursively give directories read & execute privileges:

find /path/to/base/dir -type d -exec chmod 755 {} +

To recursively give files read privileges:

find /path/to/base/dir -type f -exec chmod 644 {} +


Or, if there are many objects to process:

chmod 755 $(find /path/to/base/dir -type d)

chmod 644 $(find /path/to/base/dir -type f)


Or, to reduce chmod spawning:

find /path/to/base/dir -type d -print0 | xargs -0 chmod 755

find /path/to/base/dir -type f -print0 | xargs -0 chmod 644

Object.defineProperty

Coding — Tags: — Posted by Starck on October 6, 2013


  • writable
  • configurable
  • enumerable
  • get(getter) / set(setter)


JS Bin

Reference: Mozilla Developer

Function.bind

Coding, Log — Tags: — Posted by Starck on October 4, 2013


你可以利用 function.bind 來改變呼叫函式的對象(this): JS Bin

try starck.callBySelf(); or starck.callByWindow(); in console.


function.bind

For a given function, creates a bound function that has the same body as the original function. In the bound function, the this object resolves to the passed in object. The bound function has the specified initial parameters.

function.bind(thisArg[,arg1[,arg2[,argN]]])

Reference


Callback 的應用: JS Bin try starck.say('hi')

JIU-KUI Liquor Store Identity Design

Design — Tags: — Posted by Starck on October 2, 2013


jiukui

Design by Starck Lin

Inanna Identity Design

Design — Tags: — Posted by Starck on October 2, 2013


inanna

Design by Starck Lin

SOLID principles

Coding, Design — Tags: , — Posted by Starck on October 1, 2013


The SOLID design principles, articulated by Robert “Uncle Bob” Martin, are five principles that provide a good foundation for sound application design. The five principles are:

  • S (Single Responsibility Principle)
  • O (Open/Closed Principle)
  • L (Liskov Substitution Principle)
  • I (Interface Segregation Principle)
  • D (Dependency Inversion Principle)


Single Responsibility Principle

A class (or unit of code) should have one responsibility.

設計一個類別,所有的實作應只為用來解決一個特定意圖(需求) 而如果為了解決這個需求,而延伸出其它的需求與實作,應該把這些工作交給其它類別。


Open/Closed Principle

A class should be open for extension but closed for modification. You can extend a class or implement and interface, but you should not be able to modify a class directly. This means you should extend a class and use the new extension rather than change a class directly. Additionally, this means setting class attributes and methods as private or protected properly so they cannot be modified by external code.

一個類別應能被很有彈性地擴充,而不是直接修改其自身來解決新的意圖(需求)。以此原則來決定這個類別的屬性和方法該怎麼被限制,或該開放什麼?

ValidationModel


Liskov Substitution Principle

Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program. In PHP, this often means creating interfaces for your code to implement. You can then change (switch-out) implementations of the interfaces, Doing so should be possible without having to change how your application code interacts with the implementation. The interface serves as a contract, guaranteeing that certain methods will be available.

每當我們在一個類別引用了其它類別時,多思考如何往後如何切換或取代這件事,之後如果不用到它,會有什麼影響?如何讓更換這件事不費力? 經驗上,我們往往會遇到一些被設計用來解決相同需求(購物這個需求),但有各自實作方式(Paypal, Stripe …等支付方法)的情況,我們可以利用界面(interface),定義一個大家共同遵循的規範(我們都應該有購買這個行為),這個概念如同讓這些類別簽定了一個契約(Contract),以確保都將一定會實行哪些行為。如此,我們只要專注那些行為在流程上的安排,不管現在這個實行的對象為何都不致於讓流程出錯,甚致隨時可以將其取代成別的也簽定契約的類別。

LiskovSubstitution


Interface Segregation Principle

Many client-specific interfaces are better than one general-purpose interface. In general, it’s preferable to create an interface and implement it many times over than create a general-purpose class which attempts to work in all situations.

因為在程式語言中,一旦行為(method)在界面(interface)中被定義,在類別中就要嚴格地去實作。所以應該避免用一個很概廓性的界面讓類別去實作很多不必要的行為。 就設計的角度來看,就是思考如何拆分行為的關連性這件事。


Dependency Inversion Principle

One should depend upon abstractions rather than concrete classes. You should define class dependencies as an interface rather than a concrete class. This allows you to switch an implementation of the interface out without having to change the class using the dependency.

This principle states that high-level code should not depend on low-level code, and that abstractions should not depend upon details.

依賴界面的關係比依賴類別來得更好,目的是希望在一個系統中元件之間相互依賴的程度(耦合性)盡可能的降低,以避免牽一髮動全身的情況發生。 以這個原則來說,最常見的情況,就是該怎麼設計如何切換資料來源的機制。資料的存取可能是在 MySQL, NoSQL, 甚至是 Memory,我們如何在不影響主程式太多的情況下改變來源?

舉例來說

這段程式大概是長得像這樣的句子:

「我要去MySQL檔案庫查詢使用者名單」。

這是一個耦合性高的句子,意即我們想要稍微改變一下行為,整個「」裡的文字都要變動,所以我們稍微調整一下:

我要去 MySQL 檔案庫「查詢使用者名單」。

MySQL 檔案庫為 來源地, 而 查詢使用者名單 為動作。

雖然我把來源地區隔開來,但實際上如果我改去 NoSQL 檔案庫,那麼這個句子還是會出錯,因為我們明確指出我們要到 MySql 檔案庫,該用什麼句子來表達才不會有問題呢?

答案是使用統稱:「我」要去「檔案庫」「查詢使用者名單」。

白話一點就是叫我們講話講得愈模糊愈好,就像是如果我跟一個以上的女朋友同時在交往,每一位打電話來我都叫她「親愛的」,這樣就可以避免叫錯人的情況啦。





(c) 2024 Starck Lin | powered by WordPress